Page 1 of 1

Figuring out session variables? Having a problem

Posted: Fri Aug 24, 2012 8:49 pm
by mcc_shane
Hi Everyone,

I'm a little confused by sessions.

This is my goal. In the below URL If someone clicks on "contact us" and they aren't logged in I want that user to be re-directed to a login page. I know I can do this with php sessions but I'm a confused as to how. Does the session have to be on the car-display-contact2.php or the car-display-contact-update.php? or both pages?

How would you attack this problem?
http://whatsmyowncarworth.com/auto-memb ... ntact2.php

Thanks everyone!

Re: Figuring out session variables? Having a problem

Posted: Sat Aug 25, 2012 6:42 am
by social_experiment
For this you have to check a session that indicates a 'logged in' status; (let's say $_SESSION['loggedIn']).

Code: Select all

<?php
 session_start();
 // if this variable isn't set, or isn't equal to whatever set when 
 // a user logs in, the user isn't logged in.
 if ($_SESSION['loggedIn'] != 1) {
 // redirect user
 }
?>
Once you set the variable, regardless of page, the session variable will be available as long the session is active, so if you set $_SESSION['color'] when a user first access the page, $_SESSION['color'] will be available on each page that has session_start() at the top. The important thing is session_start() to ensure the session is continued from page to page.

Re: Figuring out session variables? Having a problem

Posted: Sat Aug 25, 2012 1:44 pm
by mcc_shane
Hey Everyone,

Thanks for your reply. Appreciate it.

I made a little progress today. I added the below syntax and a login URL link is presented. Now I'm trying to figure out after a user logs in how would I get that user back to the car-display-contact-update.php? page? What variables would I need to carry from page to page?

How would I get the visitor back to the below page after they login?
http://whatsmyowncarworth.com/auto-memb ... Contact+Us

This is the syntax I added on my car-display-contact-update.php page

Code: Select all

if (!isset($_SESSION['id'])) { 
   echo 'Please <a href="http://whatsmyowncarworth.com/auto-members/login.php">log in</a> to access your account';
   exit(); 
} 

Re: Figuring out session variables? Having a problem

Posted: Sun Aug 26, 2012 3:30 am
by simplypixie
You code use

Code: Select all

$_SERVER['HTTP_REFERER']
and store that in a temporary session variable, for example (on the login page):

Code: Select all

if (!isset($_SESSION['refURL'])) {
    $_SESSION['refURL'] = $_SERVER["HTTP_REFERER"];
}
Then once they have logged in, use that in your redirect:

Code: Select all

header ("location: {$_SESSION['refURL']}");
And then once back on the original page, clear that session variable:

Code: Select all

unset($_SESSION['refURL']);