Jump to content
phppup

PHP sessions

Recommended Posts

I have several HTML files that I've developed and have realized the need to pass a common variable between them (although the variable originates with Page1 and is not really required until Page5, it seems the most effective method would be to shuffle the data through the chain of pages).

Example: Page1.html - select the important category that you want to see information about

                 Page2.html - what is your email address etc. (does not effect CATEGORY selection)

                 Page3.html - some random information that does not effect CATEGORY selection

                 Page4.html - click link that will use the VARIABLE from Page1.html to redirect to the webpage for the CATEGORY you selected.

 

Is using SESSIONS throughout all the pages the most effective method to accomplish this?

Can I simply add <? session_start(); with the essential variable ?>   to the HTML page?   Or does it need to become a PHP page?

Is there advantage to either approach or a better alternative?

 

 

 

Share this post


Link to post
Share on other sites

Any page with php on it needs to have a .php extension unless your server is set up to parse pages with an HTML extension, which it's hopefully not (it'll slow down plain HTML pages). Beyond that, what you're describing is a basic multi-page form and yes, using sessions is a good solution to this. As to how to use sessions, the manual is a great place to start.

  • Great Answer 1

Share this post


Link to post
Share on other sites

Sessions will work just fine but I would first suggest you rethink the multi page idea. What you want to do can be done in a single page and including the other pages when required. It would be much cleaner.

  • Like 1

Share this post


Link to post
Share on other sites

To use the PHP SESSION array one simply adds this line to the beginning of the php in any and all scripts that you want to use it in.  That line looks like this:

session_start();

unless you want to do something fancy like naming the session yourself or not use all of the defaults, which is what it sounds like would work for you.  Note that I did not add the php start and end tags since they are NOT necessary on every line of php code.  Furthermore you do not need to close the session since PHP will automatically do that for you when your script exits.

Once you have the session started, you can reference any existing var and add new vars to the array simply by assigning them, as in:

$_SESSION['myvar1'] = 'xyz'.

Or:

if (isset($_SESSION['myvar1']))

    (do something with it)

else

    (do something when it is missing)

 

Share this post


Link to post
Share on other sites

Sessions can do it, by virtue of sharing data across all page requests, but here I agree with benanamen: you should use multi-page forms instead. On the second page, you put the first page's inputs (the category) into the form as a hidden input. On the third page, you have the first two pages' inputs (category and email) as hidden inputs. And so on until the last page which has its own inputs and everything before it as hidden inputs. It really isn't that much more work.

The problem with the session is that there is no sense of progression in it. It doesn't lend itself to the sequential page 1 -> page 2 -> page 3 -> page 4 that you want. I realize this is kind of vague but it's hard to describe the way sessions feel. They're a utility to store information your site needs, and that fits the bill, but the multi-page form is almost literally exactly what you want to do. It suits your purpose better. It's the right tool for the job.

Share this post


Link to post
Share on other sites
Quote

 Page1.html - select the important category that you want to see information about

                 Page2.html  ... etc.


Goodness gracious, great balls of AJAX!  What in God's green earth are you selling? Because if it's not blow, crypto-currency or heavily-discounted precious metals ain't no one got time to load a page with one data item on it.

Edited by dalecosp
#!punctuation

Share this post


Link to post
Share on other sites

In the olden days of early microsoft SDK's products and GUI standards, this idea was referred to as a "wizard".  You need to get from A -> B, with a series of decision points essentially configuring things along the way. 

The "wizard" would lead the user along the way pointing out configuration options and perhaps setting defaults for you, so that you had a next/back chain of dialogs that lead you to the same place, but in a stepwise fashion:   A -> A1 -> A2 -> B.  

The problem with the Wizard is that as soon as someone no longer needed the hand holding, the Wizard becomes an impediment that gets in the way of the power user, so in best practice.

The consideration I'd be concerned about is the person that comes to your site,  goes through much of the decision tree, then stops for whatever reason, and comes back to it 5 minutes later.  This happens ALL THE TIME.  Beware the design that forces this stepwise chain of dialogs for users that have already gone through some part of it.  While I understand the point being made by some of my esteemed fellow phpfreaks, sessions do have the advantage that they will persist for a configurable period of time, even if the user was to close out their browser entirely (assuming the default use of a session cookie is in place).

A person who has already given you their email and perhaps picked the category and seen the other screen whatever that might be, certainly should not be forced down the same path again upon return, unless you would rather not have that user as a customer.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.