Jake Huston
Jake Huston

Reputation: 71

Using $_POST for longer than 1 page

I am working on a website where the first page asks you to type in your name.

I want to use the $_POST[''] to display their name for more than just the next page after the input. How can I make it remember the $_POST['']?

Upvotes: 1

Views: 76

Answers (4)

Daryl Gill
Daryl Gill

Reputation: 5524

You can use cookies or sessions

Using Sessions:

session_start();
$_SESSION['KeyName'] = $_POST['HTMLInputName'];
$_SESSION['AnotherKey'] = $_POST['AnotherHTMLInputName'];

Then throughout your other pages:

session_start(); 
echo $_SESSION['KeyName'];

Sessions are useful but you should make modifications to your php.ini so sessions are retained for longer.

Option 2:

Using cookies:

setcookie("CookieName", $_POST['InputName']);

Then on other pages:

if (isset($_COOKIE['CookieName'])){ echo $_COOKIE['CookieName']; }

Upvotes: 0

underscore
underscore

Reputation: 6887

USE Sessions.In your First page make it like bellow

<?
session_start()

$_SESSION['name'] = $_POST['name'] ;

//your rest of codes 


?>

and other pageS

<?
session_start()

echo  $_SESSION['name'];

//your rest of codes 


?>

Upvotes: 1

Rick Burgess
Rick Burgess

Reputation: 725

Take a look at PHP Sessions:

PHP Sessions

Note: if its just names you are storing thats cool, if you start to store more private information you will want to look at using sessions in combination with database persistance.

Upvotes: 0

ricardohdz
ricardohdz

Reputation: 579

Put the name in a session variable:

$_SESSION['name']="myName";

For complete session reference see http://php.net/manual/en/book.session.php

Upvotes: 1

Related Questions