Jump to content

Recommended Posts

Hello I am using a farely basic login script for my site. But for some reason sometimes when I logout and try to log back in it will not let me login even though the login details are correct. If I then register with new login details it will let me login with those details but no previous ones. I just can't figure this out.

Here is this code

 

login.php

<form name="login_form" action="login2.php" method="post">
    <table>
        <tr><td><input type="text" name="username" id="login_fields" value="" /></td><td><input type="password" name="password" id="login_fields"  value="" /></td><td><input type="submit" name="submit" value="Login" /></td></tr>
    </table>
</form>

 

 

login2.php

<?php
			$username 	= $_POST['username'];
			$password 	= $_POST['password'];
			$submit 	= $_POST['submit'];


	if($username&&$password)
	{
				$query = mysql_query("SELECT * FROM urbex_users");


			//Code to login!

			while($row = mysql_fetch_assoc($query))
			{
				$db_username = $row['username'];
				$db_password = $row['password'];
				$db_userid = $row['user_id'];
			}

			//Check to see if they match!
			if($username==$db_username&&$password==$db_password)
			{
				$_SESSION['urbex_username']=$db_username;
				$_SESSION['urbex_user_id']=$db_userid;
				echo "<p>You have successfully logged in! Welcome back ". $db_username .".</p>";
				echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php\">";
			}
			else
				echo "<p>Login details incorrect. Please <a href=\"login.php\">Login</a> again!</p>";
	}
	else{
		echo "<p>Login details incorrect. Please <a href=\"login.php\">Login</a> again!</p>";

		}	

			?>

 

I hope some  one can help me with this, it totally has me stumpt!

Link to comment
https://forums.phpfreaks.com/topic/242783-unable-to-login/
Share on other sites

I would recommend encrypting your password before db insertion

			$username = $_POST['username'];
			$password = $_POST['password'];
			$submit = $_POST['submit'];


	if(!empty($username) && !empty($password)) //make sure that both fields are filled out..
	{
				$query = mysql_query("SELECT * FROM urbex_users WHERE username = '$username' AND password = '$password'");
$num_rows = mysql_num_rows($query);
if($num_rows > 0){ //if there is a match in the database
$row = mysql_fetch_array($query, MYSQL_ASSOC);
$userid = $row['user_id'];
$username = $row['username'];
      $_SESSION['urbex_username']=$username;
      $_SESSION['urbex_user_id']=$userid;
				echo "<p>You have successfully logged in! Welcome back $username</p>";
				echo "<meta http-equiv=\"refresh\" content=\"2;URL=index.php\">";
			}
			else
				echo "<p>Login details incorrect. Please <a href=\"login.php\">Login</a> again!</p>";
	}
}

Link to comment
https://forums.phpfreaks.com/topic/242783-unable-to-login/#findComment-1246977
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.