June 7, 200917 yr Hi all, ive been working on this for hours... and hours now, its my first attempt at a login page with sessions. the error im getting is: Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at /homepages/12/d265923897/htdocs/digitalwinter/test/login.php:10) in /homepages/12/d265923897/htdocs/digitalwinter/test/login.php on line 54 Warning: Cannot modify header information - headers already sent by (output started at /homepages/12/d265923897/htdocs/digitalwinter/test/login.php:10) in /homepages/12/d265923897/htdocs/digitalwinter/test/login.php on line 56 here is the code for the page: Line 54 and 56 are near the bottom, i have labeled them <body> <?php $dbhost = "localhost"; $dbuser = "x"; $dbpass = "x"; $username = $_POST['username']; $password = $_POST['password']; $connection = mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error()); if (isset($connection)) { mysql_select_db("x")or die("cannot select Database"); } if(isset($_POST["submit"])) { $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $sql="SELECT * FROM members WHERE username='$username'"; $result=mysql_query($sql,$connection); $count=mysql_num_rows($result); if($count==0){ echo "Username does not exist.<br /><br />"; } else { $sqltwo="SELECT * FROM members WHERE username='$username' AND password='$password'"; $resulttwo=mysql_query($sqltwo,$connection); $counttwo=mysql_num_rows($resulttwo); if($counttwo==0){ echo "Incorrect password for <b>$username</b>.<br /><br />"; } else { if($count==1 | $counttwo==1){ session_register("username"); ***LINE 54*** session_register("password"); header("location:members.php"); ***LINE 56*** }}}} ?> <form action="<? echo $php_self ?>" method="post"> <p><label>Username</label> <input type="text" name="username" id="username" /> </p> <p><label>Password</label> <input type="text" name="password" id="password" /> </p> <p> <input type="submit" name="submit" id="submit" value="Submit" /> </p> </form> </body> </html>
June 7, 200917 yr What version of PHP are you using? If you are unsure, please find out by running a simple script on your server: <?php phpinfo (); ?> The reason I'm asking is that session_register is deprecated and since PHP 4.0 + it is advisable to use the session array; session_register("username"); ***LINE 54*** session_register("password"); header("location:members.php"); ***LINE 56*** becomes $_SESSION['username'] = "Whatever"; $_SESSION['password'] = "Whatever"; header('Location: members.php'); Hope this helps, if not, just reply and we will try again!
June 7, 200917 yr The headers issue is because you are trying to set cookies after outputting data. Without going into all the complexities of HTTP Headers, content-types, output buffering, the simple solution is: You must set headers before any output. Output can be defined as; any characters before the opening <?php tag, any print/echo statements before the session_register, any error messages caused by poor code prior to the session_register. To solve this in this case, just remove the "<body> " from before your <?php tag.
June 7, 200917 yr Author Hey, thanks for replys my php version is 5.2.9 so i changed the sessions to what you said, with that i still get error: Warning: Cannot modify header information - headers already sent by (output started at /homepages/12/d265923897/htdocs/digitalwinter/test/login.php:8) in /homepages/12/d265923897/htdocs/digitalwinter/test/login.php on line 38 I think it must have something to do with what php_penguin is saying - as i moved my php into a "logincheck.php" containing only the php and no other tags etc, and had the form action set to "logincheck.php" which worked and redirected me to members.php I would really like everything within login.php so i can set the form action to "self" as i want the message to display on the actual website rather than a blank page with an error. this is my current "login.php" with the above error as i edited it a tad: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login</title> </head> <body> <?php ob_start(); $dbhost = "localhost"; $dbuser = "x"; $dbpass = "x"; $username = $_POST['username']; $password = $_POST['password']; $connection = mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error()); if (isset($connection)) { mysql_select_db("x")or die("cannot select Database"); } if(isset($_POST["submit"])) { $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $sql="SELECT * FROM members WHERE username='$username' AND password='$password'"; $result=mysql_query($sql,$connection); $count=mysql_num_rows($result); if($count==1){ $_SESSION['username'] = "username"; $_SESSION['password'] = "password"; header('Location: members.php'); } else { echo "Incorrect username or password.<br />"; }} ob_end_flush(); ?> <h1>Login</h1> <form action="<? echo $php_self ?>" method="post"> <p><label>Username</label> <input type="text" name="username" id="username" /> </p> <p><label>Password</label> <input type="text" name="password" id="password" /> </p> <p> <input type="submit" name="submit" id="submit" value="Submit" /> </p> </form> <br /> <br /> Not a member? <a href="register.php">Register here</a>. </body> </html>
June 7, 200917 yr Do what php_penguin said. Place your php code block before any of your html. You can also forget about ob_start, ob_end_flush. Also, replace this line else { echo "Incorrect username or password.<br />"; } with something like this : else { $error = "<p>Incorrect username or password.</p>"; } You can then check for $error and print it out where you like. So your code would look something like: <?php $dbhost = "localhost"; $dbuser = "x"; $dbpass = "x"; $username = $_POST['username']; $password = $_POST['password']; $connection = mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error()); if (isset($connection)) { mysql_select_db("x")or die("cannot select Database"); } if(isset($_POST["submit"])) { $username = stripslashes($username); $password = stripslashes($password); $username = mysql_real_escape_string($username); $password = mysql_real_escape_string($password); $sql="SELECT * FROM members WHERE username='$username' AND password='$password'"; $result=mysql_query($sql,$connection); $count=mysql_num_rows($result); if($count==1){ $_SESSION['username'] = "username"; $_SESSION['password'] = "password"; header('Location: members.php'); } else { $error = "<p>Incorrect username or password.</p>"; } } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Login</title> </head> <body> <?php if ($error) echo $error; ?> // wherever you want the error message <h1>Login</h1> <form action="<? echo $php_self ?>" method="post"> <p><label>Username</label> <input type="text" name="username" id="username" /> </p> <p><label>Password</label> <input type="text" name="password" id="password" /> </p> <p> <input type="submit" name="submit" id="submit" value="Submit" /> </p> </form> <br /> <br /> Not a member? <a href="register.php">Register here</a>. </body> </html>
June 7, 200917 yr Author Thanks elanman, its worked now. although ive had to cancel using the new $_SESSION code as i cant get it to work with that. i think its an issue when i try and see if there is a valid session on the member page, it does not recognise me and it goes back to registration page. on the member page this is at the very top of everything and works with the old session code. im not sure what to change it to with the new session code <?php session_start(); if(!session_is_registered(username)){ header("location:register.php"); } ?>
June 7, 200917 yr Author its ok now, ive found that with the new $_SESSION i needed to start the session on each page. eveything working so far... now to continue with the rest. Thanks for your help guys. appretiated :)
June 7, 200917 yr Should have mentioned starting the session, sorry mate! Glad you fixed your problem anyway
Create an account or sign in to comment