Reputation: 49
How do I get my Username and password from one php page that checks to see if the username and password is correct then to another which simply prints out what the user typed in on the page before.
I don't want to use 'include' because that executes my entire php file which will not work because it runs the login form twice and it then redirects me to my invalid login form page.
On a lot of website where you login in on one page then on the next page it says welcome, your name, this is pretty much what I am trying to do.
I have tried to include it, used ob_start() and then include then ob_end_clean(), after the user types they info I tried to put that in one php file, all of these didn't work. I feel it is a lot simpler than all of this but I just don't know how to do it.
Here is my php file that checks if the username and password are correct.
<?php
session_start();
$usrArray = array("Shawn","Terri","RJ","Mark");
$pwdArray = array("sjam96","terri","rj","mark");
$usr = $_SESSION['username'];
$pwd = $_SESSION['password'];
$valid = 0;
for($i=0; $i<4; $i++){
if(($usr == $usrArray[$i]) && ($pwd == $pwdArray[$i])){
$valid = 1;
break;
}
}
if ($valid > 0){
header ("Location: futurePoolPageCheck.php");
}else {
header ("Location: Invlaid.html");
}
?>
then this is where it prints it out on the next page:
<?php
session_start();
$usr = $_SESSION['username'];
echo $usr;
echo $pwd;
?>
Upvotes: 2
Views: 2606
Reputation: 580
You need to set the details into a session when the user successfully logs in. After you've run through the log in query to check the password, username, email address etc matches, place the user id in a variable such as $id=$row['id'] and include the following line:
$_SESSION['user'] = $id;
Then you can pull the results from the database on any of your pages using the session as a reference.
Alternatively, if it's only the name you're looking at displaying you could set a session as the user's name such as
$_SESSION['username'] = $row['username'];
Then you could just check if the session's set and echo it out anywhere you need to use it.
These are just basic examples, but should give you an idea what to do from there.
Upvotes: 1