jedney Posted March 5, 2008 Share Posted March 5, 2008 Hello. I am having a problem making a user authentication script. I've done about 5 scripts, and I get problems with them all, so I was wondering if anyone can help me out here, I don't want to do a new one, I need to find out what's wrong with what I have. <?php ini_set('error_reporting',E_ALL); // Start login session session_start(); $errorMessage = ''; // Set username and password variables $username = $_POST['username']; $password = $_POST['password']; // Check database for username/password $sql = "SELECT username FROM users WHERE username = '$username' AND password = MD5('$password')"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); if (mysql_num_rows($result) == 1) { // Username and Password match // Set session $_SESSION['user_logged_in'] = true; // Move to member page after login header('location: members.php'); exit; } else { echo " Login Faild"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td class=\"norm\">Your login attempt was not successfull. If you feel you have received this message in error, please contact Lev.</td></td>"; echo "</tr>"; echo "<tr>"; echo "<td></td></td></td>"; echo "</tr>"; echo "<tr>"; echo "<td></td>"; }?> When I login, I receive this error: Warning: session_start() [function.session-start]: Cannot send session cookie - headers already sent by (output started at /home/c575964/public_html/b1/processlogin.php: in /home/c575964/public_html/b1/processlogin.php on line 77 Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at /home/c575964/public_html/b1/processlogin.php: in /home/c575964/public_html/b1/processlogin.php on line 77 Also, line 77 is: session_start(); But alternatively to the session issue, the login fails. Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted March 5, 2008 Share Posted March 5, 2008 Why are you starting a session 2 times? Delete session_start() from line 77 and tells me what happens. Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 5, 2008 Share Posted March 5, 2008 make sure you put the session start as your FIRST line of code in the script. Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted March 5, 2008 Share Posted March 5, 2008 try using ob_start(); and remove the error line Quote Link to comment Share on other sites More sharing options...
frijole Posted March 5, 2008 Share Posted March 5, 2008 does anything get printed to the screen before session_start() executes? i.e. is there any HTML before you start the PHP script? Quote Link to comment Share on other sites More sharing options...
jedney Posted March 5, 2008 Author Share Posted March 5, 2008 Yes, I have a lot of HTML before the script. <?php include('connect.php'); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//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>Next Level Gaming :: A Professional Day of Defeat Gaming Team</title> <link href="style.css" rel="stylesheet" type="text/css" /> </head> <body> <table border="0" cellpadding="0" cellspacing="0" class="MainTable"> <tr> <td colspan="2"><img src="images/header.gif" alt="Next Level Gaming" width="800" height="100" longdesc="Next Level Gaming - A Professional Day of Defeat Team" /></td> </tr> <tr> <td colspan="2"> </td> </tr> <tr> <td class="MainLeftCol"><table width="175" border="0" cellspacing="0" cellpadding="0"> <tr> <td><img src="images/navhdr.gif" alt="Navigation" width="200" height="15" /></td> </tr> <tr> <td><div class="norm"><?php // DISPLAY NAVIGATION INFORMATION $query = "SELECT * FROM navigation WHERE type='1'"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)){ $link = $row["link"] ; $url = $row["url"] ; echo "<div class=\"norm\"><a href=". $row["url"] .">". $row["link"] ."</a><br /></div>"; } ?></div></td> </tr> <tr> <td> </td> </tr> <tr> <td><img src="images/dodhdr.gif" alt="Day of Defeat Resources" width="200" height="15" /></td> </tr> <tr> <td><span class="norm"> <?php // DISPLAY NAVIGATION INFORMATION $query = "SELECT * FROM navigation WHERE type='2'"; $result = mysql_query($query); while ($row = mysql_fetch_array($result)){ $link = $row["link"] ; $url = $row["url"] ; echo "<a href=\"". $row["url"] ."\">". $row["link"] ."</a><br />"; } ?> </span></td> </tr> <tr> <td> </td> </tr> <tr> <td><img src="images/serverhdr.gif" alt="Day of Defeat 1.3 Server" width="200" height="15" /></td> </tr> <tr> <td><a href="http://www.gametracker.com/server_info/8.12.19.10:27015/"> <img class="noborderimg" src="http://cache.www.gametracker.com/server_info/8.12.19.10:27015/b_160x400_T0_F-1-2-3_CFFFFFF-C5C5C5-FFFFFF-434736.png" alt="" /> </a></td> </tr> </table></td> <td valign="top" class="MainCol"><table width="600" border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="top" class="NewsHeader"> <? ini_set('error_reporting',E_ALL); // Start login session $errorMessage = ''; // Set username and password variables $username = $_POST['username']; $password = $_POST['password']; // Check database for username/password $sql = "SELECT username FROM users WHERE username = '$username' AND password = MD5('$password')"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); if (mysql_num_rows($result) == 1) { // Username and Password match // Set session $_SESSION['user_logged_in'] = true; // Move to member page after login header('location: members.php'); exit; } else { echo " Login Faild"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td class=\"norm\">Your login attempt was not successfull. If you feel you have received this message in error, please contact Lev.</td></td>"; echo "</tr>"; echo "<tr>"; echo "<td></td></td></td>"; echo "</tr>"; echo "<tr>"; echo "<td></td>"; }?> </tr> </table></td> </tr> <tr> <td colspan="2" class="MainLeftCol"><div class="copyright">Web Design & Development by Lev | © 2008, Next Level Gaming<br /> <br /> <img src="images/xhtml10.gif" alt="Validated as XHTML 1.0 Strict" width="50" height="18" /><img src="images/vcss.gif" alt="Validated as Valid CSS" width="50" height="18" /><br /> </div></td> </tr> </table> </body> </html> </body> </html> I removed the line 77 that said session_start(), but the login still fails. Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted March 5, 2008 Share Posted March 5, 2008 You have to include your connect file or how do you plan on getting the MySQL data? Try this: <?php // Start login session session_start(); // Include Connect File require('connect.php'); ini_set('error_reporting',E_ALL); $errorMessage = ''; // Set username and password variables $username = $_POST['username']; $password = $_POST['password']; // Check database for username/password $sql = "SELECT username FROM users WHERE username = '$username' AND password = MD5('$password')"; $result = mysql_query($sql) or die('Query failed: ' . mysql_error()); if (mysql_num_rows($result) == 1) { // Username and Password match // Set session $_SESSION['user_logged_in'] = true; // Move to member page after login header('location: members.php'); exit; } else { echo " Login Faild"; echo "</td>"; echo "</tr>"; echo "<tr>"; echo "<td class=\"norm\">Your login attempt was not successfull. If you feel you have received this message in error, please contact Lev.</td></td>"; echo "</tr>"; echo "<tr>"; echo "<td></td></td></td>"; echo "</tr>"; echo "<tr>"; echo "<td></td>"; } ?> Quote Link to comment Share on other sites More sharing options...
jedney Posted March 5, 2008 Author Share Posted March 5, 2008 I have that as my first line of the page, should it be a problem because it's not in that block of PHP? Quote Link to comment Share on other sites More sharing options...
Shiny_Charizard Posted March 5, 2008 Share Posted March 5, 2008 Try using the code that I gave you. Also delete the session_Start() from line 77. Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 5, 2008 Share Posted March 5, 2008 what block of php the top 2 line of the code should be <?php session_start(); Quote Link to comment Share on other sites More sharing options...
jedney Posted March 5, 2008 Author Share Posted March 5, 2008 I have multiple blocks of PHP code, throughout my XHTML. My MySQL connection information, is in another file, and is the very first line of my whole page. Below that, I have 2 blocks of PHP, which read the DB to display navigation links. The 4th block of PHP is the code for processing the login. I have removed the session_start(); from my script, but I still cannot login. Quote Link to comment Share on other sites More sharing options...
ohdang888 Posted March 5, 2008 Share Posted March 5, 2008 before you do ANYTHING do this <?php session_start(); ?> all other info. Quote Link to comment Share on other sites More sharing options...
jedney Posted March 5, 2008 Author Share Posted March 5, 2008 That line is line 1 on the page, before any other line of anything else. I am still having an issue logging in, the script keeps replying login invalid. I'm unsure what the problem could be. The password field is MD5 in the table, and the username matches. Any ideas? Quote Link to comment Share on other sites More sharing options...
jedney Posted March 5, 2008 Author Share Posted March 5, 2008 Problem Solved. I set a limit on the password field, and the MD5 encryption was passing that limit. Quote Link to comment 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.