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!
Figuring out session variables? Having a problem
Moderator: General Moderators
- social_experiment
- DevNet Master
- Posts: 2793
- Joined: Sun Feb 15, 2009 11:08 am
- Location: .za
Re: Figuring out session variables? Having a problem
For this you have to check a session that indicates a 'logged in' status; (let's say $_SESSION['loggedIn']).
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.
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
}
?>
“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
Re: Figuring out session variables? Having a problem
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
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();
} - simplypixie
- Forum Newbie
- Posts: 1
- Joined: Sun Aug 26, 2012 3:22 am
Re: Figuring out session variables? Having a problem
You code use and store that in a temporary session variable, for example (on the login page):
Then once they have logged in, use that in your redirect:
And then once back on the original page, clear that session variable:
Code: Select all
$_SERVER['HTTP_REFERER']Code: Select all
if (!isset($_SESSION['refURL'])) {
$_SESSION['refURL'] = $_SERVER["HTTP_REFERER"];
}Code: Select all
header ("location: {$_SESSION['refURL']}");Code: Select all
unset($_SESSION['refURL']);