How to Pass Variable Between Two Web Pages With PHP Without Using Session

PHP Pass variable to next page

HTML / HTTP is stateless, in other words, what you did / saw on the previous page, is completely unconnected with the current page. Except if you use something like sessions, cookies or GET / POST variables. Sessions and cookies are quite easy to use, with session being by far more secure than cookies. More secure, but not completely secure.

Session:

//On page 1
$_SESSION['varname'] = $var_value;

//On page 2
$var_value = $_SESSION['varname'];

Remember to run the session_start(); statement on both these pages before you try to access the $_SESSION array, and also before any output is sent to the browser.

Cookie:

//One page 1
$_COOKIE['varname'] = $var_value;

//On page 2
$var_value = $_COOKIE['varname'];

The big difference between sessions and cookies is that the value of the variable will be stored on the server if you're using sessions, and on the client if you're using cookies. I can't think of any good reason to use cookies instead of sessions, except if you want data to persist between sessions, but even then it's perhaps better to store it in a DB, and retrieve it based on a username or id.

GET and POST

You can add the variable in the link to the next page:

<a href="page2.php?varname=<?php echo $var_value ?>">Page2</a>

This will create a GET variable.

Another way is to include a hidden field in a form that submits to page two:

<form method="get" action="page2.php">
<input type="hidden" name="varname" value="var_value">
<input type="submit">
</form>

And then on page two:

//Using GET
$var_value = $_GET['varname'];

//Using POST
$var_value = $_POST['varname'];

//Using GET, POST or COOKIE.
$var_value = $_REQUEST['varname'];

Just change the method for the form to post if you want to do it via post. Both are equally insecure, although GET is easier to hack.

The fact that each new request is, except for session data, a totally new instance of the script caught me when I first started coding in PHP. Once you get used to it, it's quite simple though.

Pass variables between two PHP pages without using a form or the URL of page

Sessions would be good choice for you. Take a look at these two examples from PHP Manual:

Code of page1.php

<?php
// page1.php

session_start();

echo 'Welcome to page #1';

$_SESSION['favcolor'] = 'green';
$_SESSION['animal'] = 'cat';
$_SESSION['time'] = time();

// Works if session cookie was accepted
echo '<br /><a href="page2.php">page 2</a>';

// Or pass along the session id, if needed
echo '<br /><a href="page2.php?' . SID . '">page 2</a>';
?>

Code of page2.php

<?php
// page2.php

session_start();

echo 'Welcome to page #2<br />';

echo $_SESSION['favcolor']; // green
echo $_SESSION['animal']; // cat
echo date('Y m d H:i:s', $_SESSION['time']);

// You may want to use SID here, like we did in page1.php
echo '<br /><a href="page1.php">page 1</a>';
?>

To clear up things - SID is PHP's predefined constant which contains session name and its id. Example SID value:

PHPSESSID=d78d0851898450eb6aa1e6b1d2a484f1

How to pass variable between two web pages with PHP without using session

I think you should try something like below:

firstpage.php

<a href="shopping.php?identity=<?php echo $identity?>" target="blank">

secondpage.php

<p> <?php  $myvarC = $_GET['identity'];
echo $myvarC;
?> </p>

How to pass variables in page from a php script to another php script without using 'global'?

I would do it with session variables or cookies. It´s better to use session variables because you can use them anywhere in your website.

Regards.

Pass the variable to next page PHP

first you need to do put session_start() top of the script.and always remember $_SESSION is super global variable.no need to pass here to access session variable.simply you can access like this.

echo $_SESSION['name'];

Note:make sure session_start() should be top of the script.

if you really want to pass variable one page to another page.do something like below

<td> <a href="more_info.php?name=<?php echo $_SESSION['name']; ?>" >More Info</a></td> 

And your more_info.php.

if (isset($_GET['name'])) {
$name=$_GET['name'];
echo $name;
}

given answer could be solve the OP problem but.this is not good habit to use session in while loop.because you are trying to insert name in session but name could be different.i recommend to you try something like this.

Remove:

if (session_status() == PHP_SESSION_NONE) {
session_start();
}
$_SESSION['name'] = $name;
$_SESSION['phone'] = $phone;
$_SESSION['city'] = $city;

And pass the variable like this.

<td> <a href="more_info.php?name=<?php echo $name; ?>" >More Info</a></td> 

How do I share a PHP variable between multiple pages?

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

How to pass variable between php pages correctly where javascript is interactive?

Using sessions will enable pages within your site to access a 'pool' of data - providing you set session variables yourself. This can easily be done in PHP - see this page on W3 for basic details, there are plenty of other sites on the net with easy walkthroughs of sessions once you've got the basic concept.

I'm not quite sure what you're asking in question two. Is it that you want the second page to automatically know the user's country? If so, this could be achieved by assigning a session variable on the previous page, or by using geo-location - see help here

As for question three, achieving proper flow is an architectural issue - you'll need to ensure that the pages visited by the user structured to only allow the through-flow that you want. Without spending hours working through your code, I should imagine this can again be achieved through assigning session variables when a page is successfully 'completed' and ready to move to the next page.

Sessions are powerful, and will allow you to control a lot of user data to ease their experience of your site throughout their period of use. Just be careful about what session data you record though - it should always be the minimum amount possible, and not containing any data that could compromise the user's personal details, or their account - i.e. don't store their username and password, just store an int of '1' or '0' if they pass authentication.

Hope that helps.



Related Topics



Leave a reply



Submit