kam009 Posted June 13, 2008 Share Posted June 13, 2008 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> </p> <p> </p> <p> </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> Link to comment https://forums.phpfreaks.com/topic/110149-php-login-using-cookies/ Share on other sites More sharing options...
DarkWater Posted June 13, 2008 Share Posted June 13, 2008 I'd be utterly shocked if you didn't get errored out by that first script anyway. You have output before the setcookie() calls. Also, use sessions instead. =P Link to comment https://forums.phpfreaks.com/topic/110149-php-login-using-cookies/#findComment-565272 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.