Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

JackT06

macrumors 6502
Original poster
Jul 24, 2009
293
0
I followed a tour trail on Youtube about setting my Login page up. The trouble im having is that it keeep redirecting to a blankpage everytime i click on go. Here are my two codes
Please can someone help me:
PHP:
<?php
       $username = $_POST['username'];
       $password= $_POST['password'];
       $login = $_GET['login'];
       
       setcookie("username,"$username", time()+86400);
       
       
       
       if($login='yes') {
       
       
           $conn = mysql_connect("localhost","a2034243_JackT06","PASSWORD");
           
           mysql_select_db("a2034243_Member");
           
           $get = mysql_query("SELECT count(id) FROM login WHERE user='$username' and pass='$password'");
       
       $result = mysql_result($get,0);
       if($result!=1)[
       echo "Invalid login.";
       ] else[
       echo "login Successful.Welcome back" . $_COOKIE['username'] . "sir/madem.";
       $_SESSION['username'] = $username;
       ]
       }
?>

PHP:
<html>
<body>
       <form action="login.php?login=yes" method="post">
       Username: <input type="text" name="username"
       <br />
       Password: <input type="password" name="password" />
       <br />
       <input type="submit" value="Go!!" />
     </form>
</body>
</html>
 
Use the color coding of the PHP in your post to see where you may have a quote problem. hint hint.

Also if statements use {} around its content, not [].
 
Well it looks like
PHP:
setcookie("username,"$username", time()+86400);
should be
PHP:
setcookie("username",$username, time()+86400);
 
Well it looks like
PHP:
setcookie("username,"$username", time()+86400);
should be
PHP:
setcookie("username",$username, time()+86400);

Thanks for spotting that out. Changed it yet it still doing the samething it redirects me to:

file:///Users/jack/Desktop/Tester/login.php?login=yes

I can even enter random letters and it directs me to there?
 
Thanks for spotting that out. Changed it yet it still doing the samething it redirects me to:

file:///Users/jack/Desktop/Tester/login.php?login=yes

I can even enter random letters and it directs me to there?

Are you testing this locally or on a remote server? Do you have PHP error reporting turned on? Take a look at the contents of /var/log/apache2/error.log and see if it reports any errors when you load that page.

A trick would be to open command prompt, and type:

Code:
sudo tail -f /var/log/apache2/error.log
That will follow the error log and each time you refresh the page the new error log contents will output on your screen automatically.
 
Signup page redirects to nothing

This evening since about 7 non stop i have been makeing this login script. The problem that im having is that when i click submit it takes me to the page it is ment to but it does not display anything on that page.

Here are my codes if they will help
HTML:
<html>
<body>
<font size="5" face="sans-serif">Login</font>
<form action="login2.php" method="POST">
<table cellpadding="3" cellspacing="4" border="0">
<tr>
<td>Username:</td>
<td><input type="text" name="user" /></td>
</tr>
<tr>
<td>Password:</td>
<td><Input type="password" name="pass"</td>
</tr>
<td><input type="submit" name="submit" value="login"</td>
</table>
</form>

</body>

</html>

PHP:
<?php
include ("connect.php");

if ($_POST['submit']);
{
  // Get data 
  $username = $_POST['user'];
  $password = $_POST['pass'];
  
  if($username && $password)
  {
   
   $query = mysql_query("SELECT * FROM users");
   while ($getrows = mysql_fetch_assoc($query))
   {
             $dbuser = $getrows['user'];
             $dbuser = $getrows['pass'];
   
   }
   
   if (($username == $dbuser) && ($password == $dbuser)) 
   {
   echo "login Successfull..";
   }
   else
   {
   echo "Login Failure"
   }
   
   
   
  }
  else
  {
   die("The username and password fields should not be left empty.");
  }
}

?>
PHP:
<?php

$connect = mysql_connect("localhost","root") or die (mysql_error());
mysql_select_db("Website") or die (mysql_error());


?>
 
This evening since about 7 non stop i have been makeing this login script. The problem that im having is that when i click submit it takes me to the page it is ment to but it does not display anything on that page.



PHP:
<?php
include ("connect.php");

if ($_POST['submit']);
{
  // Get data 
  $username = $_POST['user'];
  $password = $_POST['pass'];
  
  if($username && $password)
  {
   
   $query = mysql_query("SELECT * FROM users");
   while ($getrows = mysql_fetch_assoc($query))
   {
             $dbuser = $getrows['user'];
             $dbuser = $getrows['pass'];
   
   }
   
   if (($username == $dbuser) && ($password == $dbuser)) 
   {
   echo "login Successfull..";
   }
   else
   {
   echo "Login Failure"
   }
   
   
   
  }
  else
  {
   die("The username and password fields should not be left empty.");
  }
}

?>

This might be your problem
Code:
if ($_POST['submit']);

Remove the ; at the end;
 
Try this for you if statement,
PHP:
if (array_key_exists('submit', $_POST))
You can also try printing out some debug messages to see where things are getting screwy. Also, do you have access to the PHP error log file? It would be very helpful probably.
 
Try this for you if statement,
PHP:
if (array_key_exists('submit', $_POST))
You can also try printing out some debug messages to see where things are getting screwy. Also, do you have access to the PHP error log file? It would be very helpful probably.

Changed it and it didn't make a difference. How do i get access to the php error file?
 
Blast.

Ok, the fact that it's not displaying anything, it must mean that the $_POST['submit'] is not being set, as you dont have an else statement for that loop.

just try appending
Code:
else {
 echo "Submit not set";
}
onto your code.


Sorry to ask a dum question but where does that need to go?
 
PHP:
<?php
include ("connect.php");

if ($_POST['submit'])
{
  // Get data 
  $username = $_POST['user'];
  $password = $_POST['pass'];
  
  if($username && $password)
  {
   
   $query = mysql_query("SELECT * FROM users");
   while ($getrows = mysql_fetch_assoc($query))
   {
             $dbuser = $getrows['user'];
             $dbuser = $getrows['pass'];
   
   }
   
   if (($username == $dbuser) && ($password == $dbuser)) 
   {
   echo "login Successfull..";
   }
   else
   {
   echo "Login Failure"
   }
   
  }
  else
  {
   die("The username and password fields should not be left empty.");
  }
}
else
{
   echo "Submit is not set";
}
?>
 
Are you doing this locally with the built-in PHP, MAMP, or a web host? It may be a logs folder named php_error.log.


To view them i put them in mamp htdocs and open them up. I think that is how i am ment to do it.
 
PHP:
<?php
include ("connect.php");

if ($_POST['submit'])
{
  // Get data 
  $username = $_POST['user'];
  $password = $_POST['pass'];
  
  if($username && $password)
  {
   
   $query = mysql_query("SELECT * FROM users");
   while ($getrows = mysql_fetch_assoc($query))
   {
             $dbuser = $getrows['user'];
             $dbuser = $getrows['pass'];
   
   }
   
   if (($username == $dbuser) && ($password == $dbuser)) 
   {
   echo "login Successfull..";
   }
   else
   {
   echo "Login Failure"
   }
   
  }
  else
  {
   die("The username and password fields should not be left empty.");
  }
}
else
{
   echo "Submit is not set";
}
?>


didnt make a difference
 
Check in MAMP/logs/ for the error log. That's where MAMP keeps the PHP error log.

Let's actually simplify some things for your script,

PHP:
<?php
include ("connect.php");

if (array_key_exists('submit', $_POST))
{
  // Get data and escape the string to protect from SQL-injection attacks
  $username = mysql_real_escape_string($_POST['user']);
  $password = mysql_real_escape_string($_POST['pass']);
  
  if ($username && $password)
  {
    $query = "SELECT user, pass FROM users WHERE user='$username' AND pass='$password'";
    $result = mysql_query($query);
    // If there's a result, we have a correct login pair
    if (mysql_num_rows($result)) {
      // login credentials correct
      echo '<h1>login success</h1>';
    }
    else {
      // incorrect login
      echo '<h1>login was not a success</h1>';
    }
  }
  else {
    // didn't fill out username and password
    echo '<p>Hey bonehead, you forgot the user name or password!</p>';
  }
}
else {
  // Output form
  echo '<p>put form here</p>';
}
?>
 
Check in MAMP/logs/ for the error log. That's where MAMP keeps the PHP error log.

Let's actually simplify some things for your script,

PHP:
<?php
include ("connect.php");

if (array_key_exists('submit', $_POST))
{
  // Get data and escape the string to protect from SQL-injection attacks
  $username = mysql_real_escape_string($_POST['user']);
  $password = mysql_real_escape_string($_POST['pass']);
  
  if($username && $password)
  {
    $query = "SELECT user, pass FROM users WHERE user='$username' AND pass='$password'";
    $result = mysql_query($query);
    // If there's a result, we have a correct login pair
    if (mysql_num_rows($result)) {
      // login credentials correct
    echo 'login success';
    }
    else {
      // incorrect login
    echo 'login was not a success';
    }
  }
}
?>

There is only two logs in there. Mysql and apache
 
There is only two logs in there. Mysql and apache

Interesting. Go to MAMP/conf/php5 and open the php.ini file. Do a search for "error_log" (w/o quotes). It'll be a line that starts with that or may have a # in front of it, which means it's commented out. It'll show where it's saving the error log file. A few lines up there'll also be a line that should have "log_errors = On" so that it's actually logging.
 
Interesting. Go to MAMP/conf/php5 and open the php.ini file. Do a search for "error_log" (w/o quotes). It'll be a line that starts with that or may have a # in front of it, which means it's commented out. It'll show where it's saving the error log file. A few lines up there'll also be a line that should have "log_errors = On" so that it's actually logging.


It says that it was allready on
 
It says that it was allready on

And the location of the error log? Is that pointing to the place I had you look for it?

You can also change settings for these lines if need be,
Code:
error_reporting  =  E_ALL
display_errors = Off # turn on if you want errors to show on page as well
If you do make a change you'll need to restart the server for them to take effect.
 
And the location of the error log? Is that pointing to the place I had you look for it?

You can also change settings for these lines if need be,
Code:
error_reporting  =  E_ALL
display_errors = Off # turn on if you want errors to show on page as well
If you do make a change you'll need to restart the server for them to take effect.

; Log errors to specified file.
error_log = "/Applications/MAMP/logs/php_error.log"

Thats where it is pointing to.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.