/    Sign up×
Community /Pin to ProfileBookmark

Help with Session Variables

i have a login form setup that redirects to another page once the person has successfully logged in. Im new to PHP and would like to set a session variable to prevent users from access the page directly via URL

For example:
The form directs to mydomain.com/supportarea/index.php

I’ve read about using session variables to track the last page. so on the form page i have the following code:

[CODE]
<?
session_start();
$_SESSION[‘lastpage’]=login;
}
?>
[/CODE]

My question is, on the page that i am redirecting to, how to i use this variable?

What i would like to do is something along the lines of, if $_SESSION[‘lastpage’]=login; then allow them to the page, if not, send them to the login page

Thanks for any help.

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@dk_zero-coolFeb 17.2010 — Well,

[code=php]session_start();
if(!isset($_SESSION['lastpage']) || $_SESSION['lastpage'] != "login") {
header("Location: login.php");
}[/code]


This will check to see if the session called "lastpage" exist and if it contains the value "login". If not, it will redirect to the login.php page. You always use two = when comparing in PHP, and one for setting a value. In my example however I use != which means NOT equivalent to.

Always use the isset() function to check arrays indexes before using them if there is a chance that it might not exist. PHP 5.3.0 and above will gennerate errors if you try to call an index that does not exist. The JavaScript style is no longer valid.

However, you can use @ in front of the variable name to kill the error instead.
Copy linkTweet thisAlerts:
@mleach82authorFeb 17.2010 — Thank you for the quick reply. Does what i need it to do, thanks again!
×

Success!

Help @mleach82 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...