1

I am asking to see if a form could have two actions? I have created a very basic login page using php where I already know the user name and password, simply having to enter them in the correctly to reach my index page, this goes through a php check page I created to ensure both are correct using the data posted through the form on the login page against the data on the check page. I want to have a welcome message on my index page displaying the name of the user however it does not seem to be working.

login page

Name <br> <input id="name" name="name" type="text"/>
Password <br> <input id="password" name="password" type="password"/>  

check page

$user=$_POST['name']; 
$pass=$_POST['password'];

if(($user==$user)&&($pass=="Steveisthebest")) {
header("Location: index.php");
}

index page

echo('Welcome to my site ' . $user . ' !');

It displays the message but just not the variable obtained from the previous login page through the post action of the form.

Any help would be appreciated, fairly new to php and still learning the ropes. Thanks.

Stukey
  • 19
  • 1
  • 2
  • 1
    Possible duplicate of [PHP Pass variable to next page](https://stackoverflow.com/questions/871858/php-pass-variable-to-next-page) – Al.G. May 09 '18 at 11:11
  • You want to look at session variables for this. If you have session_start() at the top of your script and then declare $_SESSION['user'] = $_POST['name'] it will transfer to every page with session_start() at the top. To end a session you would then use session_unset(); session_destroy(); – James May 09 '18 at 11:13

1 Answers1

2

When you redirect to the location index.php there is no longer a POST happening, that data is gone.

You need to create a session for the user when logging in. Check out the docs... http://php.net/manual/en/features.sessions.php

Building user authentication systems from scratch isn’t really advisable. There’s lots of security implications to work through. Plenty of frameworks and libraries exist.

Lastly, $user==$user is always true :)

doublejosh
  • 5,548
  • 4
  • 39
  • 45