This is for an assignment, however ive done a lot on my part to research but i feel like ive reached a wall. I need to create a page where the user can go to sign in (login.php), once they’re signed in they’re redirected to the index page. The link they clicked to login should be replaced with a logout link.
however with all this noted, first things first i do get into the session part and ive echoed the variables and retrieved them however it doesnt do the redirect to the index.php also when i manually click to the index.php after logging the session variables are empty. what am i doing wrong here???
so this is my php code in the login.php
$found = false;
//read the read.txt until the end of file
while(!feof($inputFile) && $found == false)
{
$line = fgets($inputFile);
// replace the special charater within the lines by there proper entity code
$lineArray = preg_split("/\,/", (string)$line);
if(strcmp($_REQUEST['email'],$lineArray[2]) && strcmp($_REQUEST['pwd'],$lineArray[4]))
{
$found = true;
echo "<script>alert(' FOUND!')</script>";
session_start();
$myuseremail=$_REQUEST['email'];
$mypassword= $_REQUEST['pwd'];
$_SESSION['login_email']=$myuseremail;
$_SESSION['login_pwd']=$mypassword;
setcookie("login_email", $_SESSION['login_email'], time()+60*60*24);
setcookie("login_pwd", $_SESSION['login_pwd'], time()+60*60*24);
header('Location:index.php');
}
}
fclose($inputFile);
and then in my index.php i contain this code before the body of my html
<?php
session_start();
if(isset($_SESSION['login_email']) && isset($_SESSION['login_pwd']))
{
$user_check=true;
echo $_SESSION['login_email'];
}
else
{
$user_check=false;
}
?>
within the index.php i also have this code lined in for my links
<li><a href="index.php">Home</a></li>
<li><a href="register.php">Register</a></li>
<?php
if ($user_check){
print "<li><a href='logout.php'>Logout</a></li>";
}
else{
print "<li><a href='login.php'>Login</a></li>";
}
?>
<li><a href="#"> Link 4</a></li>
I found some errors in your code, all coming down to the same point: You cannot send any custom headers after you have began outputting other data.
Where have you done this?
Here:
And here:
Personally, I think your code is a complete mess. Because I have nothing better to do, I’ll re-write it for you, explaining each step as I go along.
Let’s begin:
So the first thing you want to work on is your text file, which stores all the user details.
Instead of using plain lines or whatever, we should use JSON to split users details, from user to user.
So here’s what the text file will look like with two users in it:
Notice how I’ve also used the username as keys too and how I’ve hashed the password. So we call this file user.txt and store it somewhere safe.
Now, for the login page, we shall simply get the data through the POST method, compare it, set sessions and tell the user to go somewhere else (redirect them).
That’s all your login code, but you need your html form setup correctly for the right things to be posted with the right names. So use this html form:
That’s your login page completely sorted.
Now for your index.php:
As you did before, check if the user is logged in and throw the status is in a var:
For your HTML login/logout:
And there you have it.
Let me know if you have any problems.
One more thing:
This is far from secure. Why? You’re using text files, you’re using text files and you’re using text files.
EDIT:
To separate the JSON data by user, simply edit the text file manually (see my comment).
Or you could just paste this into your text file:
Do you see how there is no
\nin the above? Because I just created a new line manually (by just hitting enter).\nwill make the JSON code invalid, so that’s why you should avoid it. This method just means if you have to create new users, and you need a new line for each user, then you will have to do it manually.