foshoeiyyy
foshoeiyyy

Reputation: 471

How can I send a variable across php pages

I would like to create a variable in one php page and then use it another php page, is there any way this can be done?

Upvotes: 0

Views: 298

Answers (6)

Mohammad Saberi
Mohammad Saberi

Reputation: 13186

You need a session variable. Make it like this:

$_SESSION['my_var'] = 'your_value';

Then, you just need to use $_SESSION['my_var'] everywhere you need it as a global variable. Pay attention that you have to put session_start(); in the first line of your file codes everywhere you want to work with PHP sessions.

Upvotes: 0

David
David

Reputation: 219087

There are many ways to do this, depending on your setup and your needs.

  • One of the most common ways is to use session variables. This would store the value(s) server-side in a way that's accessible anywhere in the PHP application. The value will eventually time out and be lost if not persisted elsewhere, though.
  • You could store the value in a cookie. This would mean that the browser will send the value each time it accesses a page, so there's a little more network overhead. This will allow you to set the cookie to never expire so the value persists across multiple visits to the website. However, cookies are also at the mercy of the users since they're stored client-side. Some users may not allow you to set cookies, some may delete them afterward, etc.
  • You could store the value in some kind of persistent server-side storage, such as a database or even just a file. This would allow you to manage your data server-side and separate its persistence from the PHP application, since applications are more transient and not reliable for long-term data persistence.
  • You can even just add the value to hyperlinks in one page to pass it along as URL parameters to other pages. Something like: <a href="otherpage.php?value=<?php echo urlencode($someValue) ?>">click here</a> This would allow you to pass the value along to the next page and then completely forget about it after it's been used, if you don't need to keep it around for anything else.

As with many things, you have many options.

Upvotes: 1

TamDenholm
TamDenholm

Reputation: 143

Use session variables.

First of all every page you want to use sessions on needs to have the function session_start(); declared at the top of every page. You then can use the session superglobal to store things.

Page1.php:
    <?php
    session_start();
    $_SESSION['variable_name'] = 'some string';
    ?>

Page2.php:
    <?php
    session_start();
    echo $_SESSION['variable_name'];
    ?>

Page2.php will show: some string

You can store strings, ints, arrays etc. Good Luck.

Upvotes: 1

hakre
hakre

Reputation: 198247

If you're concerned about linking two PHP pages, you can just add it to a link:

one.php:

<a href="two.php?variable=<?php echo rawurlencode($variable);>"link</a>

two.php:

<?php echo htmlspecialchars($_GET['variable']); ?>

See Variables From External Sources­Docs and $_GET­Docs.

Upvotes: 0

matino
matino

Reputation: 17735

Yes, read about sessions or cookies

Upvotes: 0

Raks
Raks

Reputation: 870

you can put something in session variable, which can be accessed on any page

Upvotes: 0

Related Questions