Jump to content

Why Doesn't This Error Show Up?


Jamesking56
 Share

Recommended Posts

I have made a login script, because I have no users, it will always fail because it cant find the username to type in.BUT! for some reason, I type any username and password I click login and it just refreshes the page, The error text doesn't show??Why is this?Here is my login.php file:

<?php	include('core.php');	$page = "Login";	include('templates/header.php');	$disablesidebar = true;	include('templates/sidebar.php');		// Form Submitted?	if ( isset($_POST['login']) )	{		// Form Submitted, Check Details & Log User In		$username = $_POST['username'];		$password = $_POST['password'];				if ( !$username | !$password | $username=="" | $password=="" )		{			$error = true;			$errortext = "Please Fill In All Fields!";		}		else		{			// Check Details			$get_username = @mysql_query("SELECT username FROM users WHERE username = '$username'");			$get_password = @mysql_query("SELECT password FROM users WHERE username = '$username'");						if ( !$get_username || !$get_password )			{				// User Doesn't Exist				$error = true;				$errortext = "User Not Found!";			}			elseif ( $get_password !== md5($password) )			{				// Passwords Don't Match				$error = true;				$errortext = "Password Incorrect!";			}			else			{				// Log User In				$user_cookie = setcookie('ft_username','$get_username',time()+3600);				$pass_cookie = setcookie('ft_password','$get_password',time()+3600);								if ( !$user_cookie || !$pass_cookie )				{					// Error Making Cookies					if ( !$user_cookie )					{						// Delete Password Cookie						$cookiedelete = setcookie('ft_password','',time()-3600);					}					elseif ( !$pass_cookie )					{						// Delete Username Cookie						$cookiedelete = setcookie('ft_username','',time()-3600);					}										if ( !$cookiedelete )					{						// Cookies Not Deleted						$cookiesdeleted = false;					}										$error = true;					$redirect = true;										if ( !$cookiesdeleted )					{						// change error text						$errortext = "Cookie Error! Please Enable Cookies, Delete All Cookies then try again!";												$redirect = false;					}					else					{						// normal error text						$errortext = "Cookie Error! Could Not Create Cookies!";												$redirect = false;					}										if ( $redirect )					{						// Redirect to Security Check						echo "";					}				}			}		}	}?>		<script type="text/javascript">function check(){	if(document.loginform.username.value=="" || document.loginform.username.length==0)	{		alert('Please Fill in your Username!');		return false;	}	else if(document.loginform.password.value=="" || document.loginform.password.length==0)	{		alert('Please Fill in your Password!');		return false;	}	else	{		return true;	}	}</script>									<div id="main">								<?php									// Check if User is Logged In					if ( loggedin() )					{						$error = true;						$errortext = "You Are Already Logged In!";						$showlogin = true;					}					else					{						// Show Login Form						$showlogin = true;						$error = false;					}										if ( $error )					{						// Show Error						?>				<h2><a href="#">Error!</a></h2>				<p><?php echo $errortext; ?></p>  				<?php									}										if ( $showlogin )					{						// Show Login Form				echo "<h2><a href=\"login.php\">Login</a></h2>";									if ( $error )					{						// Error Forund with Login, Display Error						echo "<font color=red>" . $errortext . "</font><br />";					}								echo "<form method=\"post\" action=\"login.php\" name=\"loginform\">";				echo "Username: <input type=\"text\" name=\"username\" value=\"\" class=\"textbox\" />";				echo "<br />Password:   <input type=\"password\" name=\"password\" value=\"\" class=\"textbox\" /><br />";				echo "<input type=\"submit\" name=\"login\" value=\"Login!\" class=\"button\" OnClick=\"return check();\" /></p>";				echo "<p><a href=\"forgot.php\" title=\"Click here to Request a New Password!\">Forgot Your Password?</a><br />";				echo "<a href=\"register.php\" title=\"Signup Here Today!\">Signup!</a>";				echo "</form>";					}								?>			</div>				<!-- content-wrap ends here -->			</div>					<?php	include('templates/footer.php');?>

Please Help, thanks in advance!

Link to comment
Share on other sites

Basic debugging strategy. Lard your code with echo statements. Echo every variable as soon as it gets assigned anything other than a literal, even if you're "sure" you know what's going in there. You may get surprised. Especially echo your POST and GET values. (Use var_dump on arrays and objects.)But also put echo statements at key points inside control structures: if/else statements, loops, etc. In these cases, you can echo a variable, or simply echo a word that tells you where you are. As in:echo "Entered for loop<br>";The stuff that gets echoed will help you trace values and flow. Often, it's the flow that turned out weird, and you have to go figure out why. But now you know where to look.(Obviously, remove the echoes when the problem is solved.)

Link to comment
Share on other sites

I just skimmed it through and might have found the problem, you say

if ( loggedin() ){$error = true;$errortext = "You Are Already Logged In!";$showlogin = true;}else{// Show Login Form$showlogin = true;$error = false;}

basicly, your script is setting $error to true, but since they aren't logged in, it resets $error to false.If you have any other problems, try the echo thing like Deirdre's Dad said.

Link to comment
Share on other sites

Thanks! I sorted the problem!it was where I had set $error to false again! Silly Me! :) and thanks Deidres Dad I am going to use your technique in the future!Thanks! :)

Link to comment
Share on other sites

  • 3 weeks later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...