Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
747 views
in Technique[技术] by (71.8m points)

html - How do I share a PHP variable between multiple pages?

The idea/goal:

I have a username and password inside a text file on my computer. The form on the index page allows the user to sign in with their username and password. The login page is where PHP is used to validate the user entered info with the info which i have on my local text file. Directly after the user entered info match my text file info the user is redirected to a Home page where they're name is displayed with a welcome message.

The Problem:

Everything up until the validation works. The issue is that when i redirect the user to Home.php I can't display their username. How would i display their username after all the validation? Is there anyway i can permanently store their username in a variable that will be accessible across all my pages?

The Index.php page with the form

The login page that validates the form info with the info i have on my local text file

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use $_SESSION -!

session_start();
$_SESSION['username'] = $_POST['username'];

You of course want to filter/sanitize/validate your $_POST data, but that is outside of the scope of this question...

As long as you call session_start(); before you use $_SESSION - the values in the $_SESSION array will persist across pages until the user closes the browser.

If you want to end the session before that, like in a logout button --- use session_destroy()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...