Search code examples
phpweb-deployment

php variables - best way to pass variables over to another php page


So I am aware of these two common ways of passing variables across php pages.

1. PHP SESSIONS

I understand that $_SESSION is a php global variable that can hold variables across php pages during the session of the browser.

It works well but my concern with it is if a user for what ever reasons, decides to type the url of a page or goes into their history and accesses a url page for the website, the $_SESSION variable may not be set, if it was expecting the user to get to this page from a set route/path.

In addition, if a user goes into another page, and the page sets an already defined $_SESSION to another value, and then decides to go back to the previous page, the $_SESSION variable is not correct for that page, causing many errors.

2. URL passing

This is by far the most reliable in my opinion. The only concern I have with this, is the pages can get rather messy with long URL's.

page1.php?postId={variable goes here}

passing 4,5 or 6 variables can get a bit messy, I also need to encode then or encrypt them. The URL can get rather long, and I am not sure how I feel about passing variables across the URL.

My Question:

What is the best way to pass variables from one php page to another. Are the two methods above the best way to go about it, or is there another my efficient way. Also if efficiency isn't the issue, then what is the most secure procedure/method.

Thanks for your time.


Solution

  • This is largely going to depend what you're trying to do? $_SESSION variables and $_GET variables largely have different purposes in web programming (although, yes, you could force some sway between the two).

    The question you need to ask yourself is "is the variable storing information on the user OR directing the webserver to do something" if it's the former then use $_SESSION if it's the latter then $_GET.

    You wouldn't for example want to pass loggedon=true as a GET variable (ignoring the security implications) because you would have to update every single link on the page to have the query string appended to it which, as you say, would lead to some untidy URLs.

    SESSION

    Is most commonly used for storing information about a user. Some examples:

    1. Log on status
    2. Shopping basket
    3. Session preferences

    For example when a user is successfully logged on you will want some way to remember that between page loads:

    session_start();
    $_SESSION["loggedon"] = true;
    

    In every subsequent page request you can then check:

    session_start();
    if(!$_SESSION["loggedon"] ?? null){
        echo "ERROR: You shouldn't be here!";
        exit;
    }
    

    Note that $_SESSION is only accessible to the server, can't be directly accessed by the website user, and is persistent until the session closes.

    GET

    On the other hand is sent with every request and is typically used when you want to pass non-sensitive information from the user to the webserver. Some examples:

    1. Language preferences
    2. User input (e.g. a search query when using a search engine)
    3. Forgotten password secure codes

    Suppose you have a cookery website and 1000 recipes. You would likely only have one page to show the recipe and pass a GET variable in the URL to indicate which recipe should be loaded

    http://www.mycookingwebsite.com/recipe.php?recipeid=477
    

    Note that GET requests are visible to the user, can be modified, and show up in history etc. as well.

    N.B. Do not pass sensitive details (e.g. username/password) over GET - not least because they would show up in the browser history!


    You mention passing variables from one page to another. But I'm not quite clear on whether you mean Server->Server (SESSION) OR Client->Server(GET)?

    An example of this all coming together would be in the case of a shopping cart:

    At the back end you have an array stored in your session with the items in the cart, this is persistent throughout the session. On the client side you have the ability to send a GET (most people would probably POST) request to tell the server about the new product you want to add to the list.


    If your primary concern is that users may find themselves at the wrong "stage" then I suggest building in some checks to make sure that they are in the right place at the right time.

    For example given a quiz with 10 questions... If the user clicks a link which drops them at question 5 you check to see if they've already answered questions 1-4 and then act appropriately depending on the answer.