Figuring out session variables? Having a problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcc_shane
Forum Newbie
Posts: 22
Joined: Sat May 12, 2012 1:47 pm

Figuring out session variables? Having a problem

Post 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!
User avatar
social_experiment
DevNet Master
Posts: 2793
Joined: Sun Feb 15, 2009 11:08 am
Location: .za

Re: Figuring out session variables? Having a problem

Post 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.
“Don’t worry if it doesn’t work right. If everything did, you’d be out of a job.” - Mosher’s Law of Software Engineering
mcc_shane
Forum Newbie
Posts: 22
Joined: Sat May 12, 2012 1:47 pm

Re: Figuring out session variables? Having a problem

Post 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(); 
} 
User avatar
simplypixie
Forum Newbie
Posts: 1
Joined: Sun Aug 26, 2012 3:22 am

Re: Figuring out session variables? Having a problem

Post 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']);
Post Reply