I have made a log in script with some Google help but the problem is every time i refresh the page it gets log out and redirect me to the log in page. So basically i want this that a user is logged in after entering his details and not get logged out after a single refresh. my code is:
<?php
if(!isset($_SESSION)){
session_start();
}
$username = mysqli_real_escape_string($con, $_POST['username']);
$password = mysqli_real_escape_string($con, $_POST['password']);
if ($username && $password)
{
$result = mysqli_query($con, "SELECT * FROM admin WHERE username='$username' ");
$numrows = mysqli_num_rows($result);
if($numrows !=0)
{
while($row = mysqli_fetch_assoc($result))
{
$dbusername = $row["username"];
$dbpassword = $row["password"];
}
if($username==$dbusername && $password==$dbpassword)
{
$_SESSION['username'] = $dbusername;
}
else
echo "Incorrect Password";
}
else
header("location: login.php");
}
else
header("location: login.php");
?>
mysqli_real_escape_string()
REQUIRES you to have an active/established connection to the DB. Since you're doing them_r_e_s()
call BEFORE you connect, you'll simply get back boolean FALSE to signify failure. So you're trashing your "quoted" values.Boolean false values inserted into a string just get converted into empty strings, so your queries start looking like
Your code sequence should be:
And since you're using mysqli, why are you manually escaping variables anyways? You could just use a prepared statement + placeholders and bypass the problem entirely.
LEARNING POINT # 1: SESSIONS
Here are some learning points for you in regards to sessions and how to use them efficient and effectively. Please note the problem with your code is you are asking if a session was set before calling session start, so the superglobal $_SESSION is not available and therefore your logical statement always returns false, except for when you call session start in login, where you should never call session start anyway. Call session start 1 time in a config file(make sure you include this file in all of your "views/pages" then you can check if a session is set as normal. read this code very carefully and attempt to understand everything it is doing so that you can take advantage of the power that php's sessions offer.
add to login.php
add to your config.php or any file that is included in all pages
now in your files to test if a user is loggedIn
now if you need to call this object from inside the function there are two ways
other considerations
consider adding a sitewide secret property to your loggedInUser object and check for the presence of this secret to identify a valid session. it is possible to create a variable called $loggedInUser if a user isn't logged in by passing a malformed get request. odds are that the attacker won't be able to guess your site secret though. an example of this sitewide secret is shown in the first example.
LEARNING POINT # 2: A crash course to Object Oriented Mysqli
creating the connection object.
setting the charset
running a query
prepared statements: an alternative to escaping values manually