Jump to content

Log In Script


TheFilmGod

Recommended Posts

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??

Link to comment
Share on other sites

 

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 -->

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

if(!$_POST['login']{1})die('Please enter a username!');

..

 

try

 

if ( $_POST['login']!=1 ) { die('Please enter a username!'); }

 

proper syntax :D

Way to go. Don't let them register unless their name is "1".

Mine worked perfect, your "proper syntax" is broken. o_O

Link to comment
Share on other sites

if(!$_POST['login']{1})die('Please enter a username!');

..

 

try

 

if ( $_POST['login']!=1 ) { die('Please enter a username!'); }

 

proper syntax :D

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.