ronan23 Posted March 20, 2013 Share Posted March 20, 2013 I am a beginner in php so bear with me. Whenever i try to sign in with a registered user on sign-in page I get these warnings, if you could help me I would be very grateful. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /users/csdipact2013/rs7/public_html/donegalSignIn.php:12) in /users/csdipact2013/rs7/public_html/donegalSignIn.php on line 75Warning: session_regenerate_id() [function.session-regenerate-id]: Cannot regenerate session id - headers already sent in /users/csdipact2013/rs7/public_html/donegalSignIn.php on line 76 Session id = 199e7bf07b314939feca59cfadae236b Warning: Cannot modify header information - headers already sent by (output started at /users/csdipact2013/rs7/public_html/donegalSignIn.php:12) in /users/csdipact2013/rs7/public_html/donegalSignIn.php on line 80 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <?php /* ********************************************************* Main content of the page ********************************************************* */ //this will tell us whether the input data was valid $validinput = true; //if this is true at the end, then show the form to the user //but if it is false, we won't show it $show_form = false; //initialise the values that the user will give us $email = ''; $pass = ''; //initialise an array of error messages for displaying later $errors = ''; //check the value of the submit field - //if it is already set, the user must have submitted the form if (isset($_POST['submit'])) { //read and store the data from the form $email = trim($_POST['email']); $pass = trim($_POST['pass']); if (empty($email)) { $errors = '<p>You didn\'t give us your email address. Why not? Don\'t you like us? You can\'t sign in without it!</p>'; $validinput = false; } if (empty($pass)) { $errors = '<p>You didn\'t enter your password. You can\'t sign in without it.</p>'; $validinput = false; } if ($validinput == true) { //if the email and password match the database, //then we can sign them in. //open the connection to the database $dbc = mysqli_connect('cs1.ucc.ie', 'rs7', 'shainiij', 'csdipact2013_rs7') OR die('Cannot connect'. mysqli_connect_error()); //check the number of users with that address : //if <1, don't sign in $query = 'SELECT userid, fullname, email FROM fanmembers WHERE email="' . $email . '" AND pass = SHA1("' . $pass . '")'; $result = @mysqli_query($dbc, $query); $number = @mysqli_num_rows($result); if ($number > 0) //we have found the user's details { $row = @mysqli_fetch_array($result); session_start(); session_regenerate_id(); echo '<p>Session id = ' . session_id() . '</p>'; $_SESSION['userid']=$row['userid']; $_SESSION['username']=$row['fullname']; header('Location:donegalHome.php'); } else //couldn't find a match in the DB { $errors = '<p>ERROR: couldn\'t find email and password.</p>'; $show_form = true; } mysqli_close($dbc); } else { $errors = '<p>ERROR: you must supply an email and password.</p>'; $show_form = true; } } else //somehow got here without submitting, so show the form { $show_form = true; } if ($show_form == true) { //the title that will appear in the Windows window bar $pagetitle = 'Donegal Fansite Sign-In Page'; //include the standard JEdward page header include('donegalHeader.html'); echo $errors; //either there was no data submitted //or the data wasn't valid //so we are showing the form again to get fresh input //we temporarily quit php to show the form in html ?> <fieldset> <form action="donegalSignIn.php" method = "post"> <p> What is your email address?: <input type="text" name="email" size="20" maxsize="30" value="<?php echo $email; ?>"/> </p> <p> Please enter your password: <input type="password" name = "pass" size = "20" maxlength="20"/> </p> <p> <input type="submit" name="submit" value="Submit" /> </p> </form> </fieldset> <?php //back into php, and closing the bracket for if($show_form == true) } /* ********************************************************* End of main content of the page ********************************************************* */ //include the standard footer information include('donegalFooter.html'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/275922-sessions-problem/ Share on other sites More sharing options...
PaulRyan Posted March 20, 2013 Share Posted March 20, 2013 You should start the session at the very top of the page, I.E. before anything else except the PHP opening tags. <?PHP session_start(); /// Rest of code below Quote Link to comment https://forums.phpfreaks.com/topic/275922-sessions-problem/#findComment-1419826 Share on other sites More sharing options...
davidannis Posted March 20, 2013 Share Posted March 20, 2013 Paul Ryan is absolutely correct. The reason is that the session_start() wants to read/set a cookie with a session identifier. If the script has already started to output back to the browser it has already sent the header which should contain the cookie. Quote Link to comment https://forums.phpfreaks.com/topic/275922-sessions-problem/#findComment-1419828 Share on other sites More sharing options...
ronan23 Posted March 20, 2013 Author Share Posted March 20, 2013 Thanks for the responses when I put session_start() after <?php I get the same warnings. Should I modify this section of my code? Thanks if ($number > 0) //we have found the user's details { $row = @mysqli_fetch_array($result); //session_start(); session_regenerate_id(); echo '<p>Session id = ' . session_id() . '</p>'; $_SESSION['userid']=$row['userid']; $_SESSION['username']=$row['fullname']; header('Location:donegalHome.php'); } Quote Link to comment https://forums.phpfreaks.com/topic/275922-sessions-problem/#findComment-1419836 Share on other sites More sharing options...
PaulRyan Posted March 20, 2013 Share Posted March 20, 2013 (edited) Did you put the code BEFORE the html content too? <?PHP //### Start the session session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> Edited March 20, 2013 by PaulRyan Quote Link to comment https://forums.phpfreaks.com/topic/275922-sessions-problem/#findComment-1419868 Share on other sites More sharing options...
ronan23 Posted March 21, 2013 Author Share Posted March 21, 2013 I am no longer getting the warnings, it redirects to the home page but when it does go the homepage there is meant to be extra menu buttons available. Instead it is set to the default number of menu buttons. This is the menu code: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body <?php $signoutbtn = '<a href="donegalSignOut.php"><font color="green">Sign Out</font></a>'; $signinbtn = '<a href="donegalSignIn.php"><font color="green">Sign In</font></a>'; $regbtn = '<a href="donegalReg.php"><font color="green">Register</font></a>'; $homebtn = '<a href="donegalHome.php"><font color="green">Home</font></a>'; $shopbtn = '<a href="donegalShop.php"><font color="green">Shop</font></a>'; $blogbtn = '<a href="donegalBlog.php"><font color="green">Blog</font></a>'; $pappedbtn = '<a href="donegalUpload.php"><font color="green">Upload Photo</font></a>'; $viewbtn = '<a href="donegalView.php"><font color="green">View</font></a>'; $accountbtn = '<a href="donegalChangePassword.php"><font color="green">Change Password</font></a>'; $boardsbtn = '<a href="donegalMessageBoards.php"><font color="green">Message Boards</font></a>'; $memberbtn = '<a href="donegalMembers.php"><font color="green">Members</font></a>'; if (isset($_SESSION['username'])) { ?> <form action="Search.php" method="get"> <?php $username = $_SESSION['username']; echo '<p><small>' . $username . '</small> '; $menubuttons = array( 'signout' => $signoutbtn, 'home' => $homebtn, 'shop' => $shopbtn, 'blog'=> $blogbtn, 'papped' => $pappedbtn, 'view' => $viewbtn, 'account' => $accountbtn, 'boards' => $boardsbtn, 'members' => $memberbtn); foreach ($menubuttons as $key => $button) { if ($thispage == $key) { echo ' <strong>' . $button . '</strong>'; } else { echo ' ' . $button; } } //end of foreach menubutton if ($thispage != 'shop') { ?> <input type="text" name="searchstring" size="15" maxsize="30"> <input type="submit" name="submit" value="search"> <?php } echo '</form>'; } //end of isset($_COOKIE... else //so user has either not logged in, or never registered { echo '<p>'; $menubuttons = array( 'signin' => $signinbtn, 'regn' => $regbtn, 'home' => $homebtn, 'shop' => $shopbtn, 'blog'=> $blogbtn, 'members' => $memberbtn); foreach ($menubuttons as $key => $button) { if ($thispage == $key) { echo ' <strong>' . $button . '</strong>'; } else { echo ' ' . $button; } } //end of foreach menubutton } echo '</p>'; echo '<hr>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/275922-sessions-problem/#findComment-1420170 Share on other sites More sharing options...
PaulRyan Posted March 21, 2013 Share Posted March 21, 2013 You need to put session_start() at the top of that page too, any page that uses the session, you need that piece of code at the top of the page. Also, you need to turn on error reporting, because you should be get an undefined error for "$thispage", unless you have excluded that piece of code from the code posted? Quote Link to comment https://forums.phpfreaks.com/topic/275922-sessions-problem/#findComment-1420172 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.