2

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;
?>
5
  • 1
    At what point did you assign them into the session? I see you reading from it $usr = $_SESSION['username'] but I don't see you writing to it: $_SESSION['username'] = $usrArray[0] (Shawn). Commented Nov 17, 2012 at 14:40
  • Look at girlswhogeek.com/tutorials/2007/… Commented Nov 17, 2012 at 14:40
  • 1
    Is there a reason you want to store passwords in the session? That is generally not a good idea and there is almost always another way to do it. Commented Nov 17, 2012 at 14:41
  • The only reason I am storing them in a session is because I thought that is the way to get it from file to file but I used to store it in a post so maybe I should go back to that Commented Nov 17, 2012 at 14:50
  • Michael, I think I don't want to set it equal to $usrArray[0] because I want to check to see if $usr and $usrArray are the same which I do in my if statement. Thanks, Shawn Commented Nov 17, 2012 at 14:53

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.