FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » General » PHP discussions » Cookies with Php Login
Show: Today's Messages :: Unread Messages :: Show Polls :: Message Navigator
| Subscribe to topic | Bookmark topic 
Switch to threaded view of this topic Create a new topic Submit Reply
Cookies with Php Login [message #41789] Fri, 13 June 2008 00:45
kam009 is currently offline  kam009   Canada
Messages: 1
Registered: June 2008
Karma: 0
Junior Member
add to buddy list
ignore all messages by this user
Hi, i have two pages, one login and one index. Once a user logs in, two cookies are set (user and pass) and the user is redirected to index.php but here comes the problem, on the index page, the cookie is not detected at all, as if it is not even set. please help.

here is the code.

LOGIN.PHP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table align="center" width="900" border="0">
  <tr> 
    <td>HEADER GO HERE</td>
  </tr>
  <tr> 
    <td>Navigation GO HERE</td>
  </tr>
  <tr> 
    <td><form method="post" name="login" action="login.php">Login: <br> Username:<input name="username" type="text" maxlength="16"><br>Password:<input name="password" type="password" maxlength="8"><br><input type="submit" name="submit" value="Login"></form>
      <?
		$con = mysql_connect("localhost","USERNAME","PASSWORD");
		if (!$con)
		  {
		  die('Could not connect: ' . mysql_error());
		  }
		// Select the database
		$db = mysql_select_db("TABLENAME", $con);
		
		//check if the cookie is already set.
		if (isset($_COOKIE['user_id']))
		{
			// Then check its data with the database.
			$username = $_COOKIE['user_id'];
			$pass = $_COOKIE['pass_id'];
			$check = mysql_query("SELECT * FROM users WHERE username = '$username'", $con) or die(mysql_error());
			while ($data = mysql_fetch_array($check))
				{
				if ($pass != $data['password'])
						{}
				else
						{header("location: index.php");}
				}
		}
				
		//check table if data exist.
		if (isset($_POST['submit']))
			{if (!$_POST['username'] | !$_POST['password'])
				{echo "Fill in the form please.";}
			else
				{$check= mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."' AND password = '".$_POST['password']."'", $con); //grabs results from table.
		
				if (mysql_num_rows($check)>0)
					{setcookie("user_id",$username,time()+3600);
					setcookie("pass_id",$pass, time()+3600);
					header('location: index.php');
					exit();}
				else
					{echo "Check your usename and Password.";}	
				}
			}
		else
			{}
		?><br><br><a href="forgot.php">Forgot Password?</a><br><a href="register.php"><font color="#FF0000">REGISTER 
      NOW FREE</font></a></td>
  </tr>
  <tr> 
    <td>ADS GO HERE (optional)</td>
  </tr>
  <tr> 
    <td>FOOTER GO HERE</td>
  </tr>
</table>
</body>
</html>


INDEX.PHP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body onLoad="MM_preloadImages('images/login2.gif')">
<table align="center" width="900" border="0">
  <tr> 
    <td colspan="2">HEADER GO HERE</td>
  </tr>
  <tr> 
    <td colspan="2"><table border="0" cellpadding="0" cellspacing="0">
        <tr>
          <td><?
		  $con = mysql_connect("localhost","USER_NAME","PASS");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
// Select the database
$db = mysql_select_db("DATABASE_NAME", $con);
//check if cookie is already set and check if the cookie has valid data
if (isset($_COOKIE['user_id']))
  {	$username = $_COOKIE['user_id']; 
	$pass = $_COOKIE['pass_id'];
	$check = mysql_query("SELECT * FROM users WHERE username = '$username'", $con) or die(mysql_error()); 
while ($data = mysql_fetch_array( $check ))
		{ if ($pass != $data['password'])
			{echo "Welcome guest! ";
			echo "<font color='#FF0000'><b><a href='login.php'>Login Here</a></b></font>";}
		 else
			{echo "Welcome " . $username . "!<br />";}
		}
	}
else
  {echo "Welcome guest! ";
  echo "<font color='#FF0000'><b><a href='login.php'>Login Here</a></b></font>";}
?>
</td>
        </tr>
      </table></td>
  </tr>
  <tr> 
    <td><p>NEWS GO HERE</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p></td>
    <td>Brief stats go here</td>
  </tr>
  <tr> 
    <td colspan="2">ADS GO HERE (optional)</td>
  </tr>
  <tr> 
    <td colspan="2">FOOTER GO HERE</td>
  </tr>
</table>
</body>
</html>
Quick Reply
Formatting Tools:   
  Switch to threaded view of this topic Create a new topic
Previous Topic: Please, endorse my new php class library and get license free
Next Topic: PHP Development Team
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Mon Nov 24 16:20:06 EST 2014

Total time taken to generate the page: 0.00726 seconds