TheFilmGod Posted September 3, 2007 Share Posted September 3, 2007 I just finished a log in script. Getting it to work was a nightmare but I finally got it to work. But now I have a bigger problem!! If you submit a not-existing name for "username" instead of getting a clean error by the php script and echoing it on the screen, the script pukes out and gives me a blank. Absolutely nothing. Zip. Nada. Crap! What is going on here? Is this some php bug or something? This is annoying me!! Try it out: wwpknights.com At the top right write in test for "username" and test for "password". Then click "login" YOu should get a blank screen. What's going on?? Quote Link to comment Share on other sites More sharing options...
TutorMe Posted September 3, 2007 Share Posted September 3, 2007 It might help if you posted a little bit of code. Specifically the part where it checks the username and password. Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted September 3, 2007 Author Share Posted September 3, 2007 Error is all the way at the bottom <?php // Must start session to check session_start(); // Check if session is already in progress if ( isset($_SESSION['success'])) { $login = true; } // Session not in progress else { // If login form submitted // Keep $_POST['login'] in place if ( isset($_POST['login'])) { // If submitted username and password are not empty if ( !empty($_POST['login_user']) && !empty($_POST['login_pass'])) { // Connects the script to mysql require_once($element.'connect.php'); // Selects the row with the submitted username $login_check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['login_user']."'")or die(mysql_error()); // Counts the number of rows $login_check2 = mysql_num_rows($login_check) or die(mysql_error()); // If user exists if ($login_check2 == 1) { // Fetch the data row from the database $login_row = mysql_fetch_assoc( $login_check ); // Set new varaiables for the data $login_pass = $login_row['password']; $login_level = $login_row['level']; // Encrypt password so we can check it $login_pass2 = md5($_POST['login_pass']); // If submitted password matches password on record if ($login_pass == $login_pass2) { // Create session success $_SESSION['success'] = true; // Set login to true $login = true; // Create other session variables $_SESSION['level'] = $login_level; $_SESSION['user'] = $_POST['login_user']; } // Passowrds don't match else { $login_error3 = "The password you submitted is incorrect. Please try again or go to <a href=\"forgot.php\">forgot password</a>."; } } // User doesn't exist else { $login_error2 = "The user you submitted doesn't exist. Please <a href=\"register.php\">register</a>."; } } // Submitted info was empty else { $login_error1 = "You left a field blank. Pleae fill in all fields."; } } // Login form not submitted else { $login = false; } } ?> HTML/PHP to see the code: <div id="topPanlogin"> <?php // If logged in if ( $login == true ) { $user = $_SESSION['user']; $level = $_SESSION['level']; echo "<table width=\"200\" border=\"0\"> <tr> <td align=\"center\"><span class=\"login\">Logged in as: $user - $level</span></td> </tr> <tr> <td align=\"center\"> <a href=\"http://www.wwpknights.com/myprofile\">My Profile</a> | <a href=\"http://www.wwpknights.com/logout\">Log Out</a></td> </tr> </table>"; } // Not logged in else { ?> <form action="<?php echo $_SERVER['PHP_SELF']?>" method="post"> <table width="200" border="0"> <tr> <td width="96"><span class="login">User</span><br /> <input type="text" name="login_user" maxlength="40" size="15" /></td> <td width="94"><span class="login">Password</span><br /> <input type="password" name="login_pass" maxlength="50" size="15" /></td> </tr> <tr> <td colspan="2" align="center"><input type="submit" name="login" value="login" /> <?php if ( isset($login_error1) || isset($login_error2) || isset($login_error3)) { echo "<span class=\"login\">$login_error1 $login_error2 $login_error3</span>"; } else { echo "<a href=\"http://www.wwpknights.com/register\">Register</a><span class=\"login\"> | </span><a href=\"http://www.wwpknights.com/forgot\">Forgot Password</a>"; } ?> </td> </tr> </table> </form> <?php } ?> </div> <!-- Log In End --> Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 How about if(!$_POST['login']{1})die('Please enter a username!'); Or something like that? Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted September 3, 2007 Author Share Posted September 3, 2007 Although that is a good idea, I don't understand why my script doesn't work. Everything else works except that on error message. If you go to wwpknights.com and at the top right type in "test" and "test" and press log in it should puke a white page out at you. NO errors no code! Just a white page. WTF? Did this ever happen to you? Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted September 3, 2007 Author Share Posted September 3, 2007 Alright. I find out that the blank page means php crashed. I dont understand why. Since I"m on shared hosting I don't have access to log files which sucks big time. Can anyone go through my script and tell me what I did wrong or what caused the script to crash php. It really shouldn't have crashed it... Quote Link to comment Share on other sites More sharing options...
d22552000 Posted September 3, 2007 Share Posted September 3, 2007 if(!$_POST['login']{1})die('Please enter a username!'); .. try if ( $_POST['login']!=1 ) { die('Please enter a username!'); } proper syntax Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted September 3, 2007 Author Share Posted September 3, 2007 Well that isn't the problem. It seems that php crashes because of this: // Selects the row with the submitted username $login_check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['login_user']."'")or die(mysql_error()); // Counts the number of rows $login_check2 = mysql_num_rows($login_check) or die(mysql_error()); // If user exists if ($login_check2 == 1) { Now you see, the script works if the mysql_query("SELECT... actually selects a row or two. BUT if the username doesn't exists and mysql doesn't select a record for some reason "mysql_num_rows()" functions crashes. Its a though you can't count the number of rows if there isn't a selection in place. Is there a way around this? Although the way I have it should be perfectly okay. Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 if(!$_POST['login']{1})die('Please enter a username!'); .. try if ( $_POST['login']!=1 ) { die('Please enter a username!'); } proper syntax Way to go. Don't let them register unless their name is "1". Mine worked perfect, your "proper syntax" is broken. o_O Quote Link to comment Share on other sites More sharing options...
TheFilmGod Posted September 3, 2007 Author Share Posted September 3, 2007 if(!$_POST['login']{1})die('Please enter a username!'); .. try if ( $_POST['login']!=1 ) { die('Please enter a username!'); } proper syntax Way to go. Don't let them register unless their name is "1". Mine worked perfect, your "proper syntax" is broken. o_O Thanks AZU for pointing that out. But I'm not stupid enough to use that code. All I want to know is there an alternative way of checking if a username is existant? Quote Link to comment Share on other sites More sharing options...
Azu Posted September 3, 2007 Share Posted September 3, 2007 There are other IFs that can do the same thing, but I'm pretty sure this is the fastest way that is sure to always work right. Of course, you can replace the die() with whatever you want, to have it do whatever actions you want. 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.