Link to home
Start Free TrialLog in
Avatar of William Peck
William PeckFlag for United States of America

asked on

How to use $_SESSION variables in PHP between web pages.

Need to understand $_SESSION variables in php (2 weeks into wrestling with php for the first time)

Trying to implement Google login to a student survey, accessed via gmail. Prior version worked with an LDAP login, using the variable username.

The google login code returns $userid. Then I was told to use $_SESSION['valid_user'], so I tried this:  $_SESSION['valid_user'] = $userid;. But haven't been able to get this to work.

Mainly I need to populate $username with $_SESSION['valid_user'] and use that for several MySQL statements.

For example - current MySQL code
-          $query = "SELECT * FROM sum_training WHERE student_id = ' ".$username." ' ";
-          $query = "INSERT INTO sum_training (student_id_with_tng_cycle, student_id, date_submitted) VALUES( '$username', '$student_id', '$timeOnly');";

I ended up with this for username: $_SESSION[2a, where it should have been sjones2a

Original code:    sessionStorage.setItem("student_id_with_tng_cycle", "<?php print($username) ?>");

Also, having a hard time debugging this, so if you have a good php debugging link at your fingertips, that will really help.
ASKER CERTIFIED SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of William Peck

ASKER

gr8gonzo - fantastic, thank you! very helpful

hielo - thank you as well!

double checking everything now ...
Regarding the debugging part of your question.

When you're developing code, ALWAYS turn on error_reporting. Without it, your code becomes a black box and will often fail without ever telling you why. You can do that by simply adding the following to the very top of all your scripts:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

Open in new window

Now whenever there is an issue with your code, you will get to see the error messages.

Once you've done that, the next best thing to learn when debugging is the var_dump() method. This basically outputs all the information about your data, so in your code, you could do something very simple like this:

<?php
session_start();
var_dump($_SESSION);

Open in new window

When you run your script, you will get the output of everything that you have stored in the SESSION. Learn to use it a LOT when you're developing, particuarly when you're just starting out.

There are more advanced ways of debugging (xdebug / logging for example), but var_dump and error_reporting will get you going in the right direction.
got it working!!!!!!! Thank youuuuuuu :-)

I'm just taking over this code and learning php on the fly along with putting some WD-40 on my HTML skills.

So the explanation was spot on the money, and the example got me going, then as I was looking around, session_start(); was comment out in all the main files ---- grrrrr..

worked first time too ... bingo.

But good to go, thx
Chris, just saw your comment, thanks as well - very helpful.