Stukey
Stukey

Reputation: 19

Using PHP variables in more than one page?

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.

Upvotes: 1

Views: 55

Answers (1)

doublejosh
doublejosh

Reputation: 5774

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 :)

Upvotes: 3

Related Questions