Page 1 of 1
php redirect and login
Posted: Thu Dec 04, 2008 9:26 am
by eracha
hi guys,
I am quite new to php having a problem here which may sound very simple to everyone but has taken me hostage for the past few days.
I recently added a login application for my website and then only added this code
Code: Select all
<?php
header("Location: myloginpage.php");
?>
to all my webpages to redirect all users to the login page.
But then I realised I could not navigate the pages because each time I opened a page I was redirected to the login page without checking if I am logged in or not, and when I logged in, it took me to the very first page and not the one I requested.
I will highly appreciate any help on how handle this. I am dummy and finding really had to figure this out.
Thanks
Re: php redirect and login
Posted: Thu Dec 04, 2008 9:33 am
by papa
Are you using session variables?
Say that when a user logs in your store $_SESSION['user_name'];
You can then validate that on your pages and redirect the visitor depending on wether he/she is logged in or not:
Code: Select all
if(isset($_SESSION['user_name'])) {
//user logged in
header("Location: whatiwanttosee.php");
} else {
header("Location: myloginpage.php");
}
Room for improvement

Re: php redirect and login
Posted: Thu Dec 04, 2008 10:00 am
by eracha
Thanks papa for that fast reply. You answer post answer exactly my problem. Its just what I want, but i can't get it right.
here is my login script, and I don't find anywhere the session variables are defined. I am trying to customize the app. and I can't really figure things out given my limitted php knowledge.
---
function run()
{
$credentials = array();
// Get the user supplied credentials
$credentials[LOGIN_ID_FIELD] = getUserField('loginid');
$credentials['password'] = getUserField('password');
$referer = getUserField('referer');
if($referer == 'home_client_login' ||
$referer == 'home_client_post_jobs' ||
$referer == 'home_client_search_resume')
{
// Create a new Client object with the credentials
$thisUser = new Client($credentials);
}
else if($referer == 'home_candidate_login')
{
$thisUser = new Candidate($credentials);
}
else
{
// Create a new user object with the credentials
$thisUser = new User($credentials);
}
// Authenticate the user
$ok = $thisUser->authenticate();
// If successful (i.e. user supplied valid credentials)
// show user home
if ($ok)
{
$thisUser->goHome();
}
// User supplied invalid credentials so show login form
// again
else
{
//echo '<script>alert(2)</script>';
$data = array();
$data = array_merge($_REQUEST, $data);
$data['referer'] = $referer;
echo createPage(LOGIN_TEMPLATE, $data);
}
return true;
}
} // End class
If I understand, the script you have written should replace my previous redirect script on my webpages, but how and where should I define the $_SESSION['user_name']?
Many thanks
Re: php redirect and login
Posted: Thu Dec 04, 2008 11:38 am
by eracha
I tried this and it just opened the page with or without loggin:
Code: Select all
session_start()
if(isset($_SESSION['user_name'])) {
# //user logged in
header("Location: whatiwanttosee.php");
} else {
header("Location: myloginpage.php");
}
please rescue me.
Re: php redirect and login
Posted: Thu Dec 04, 2008 3:25 pm
by Syntac
Missing a semicolon.
Re: php redirect and login
Posted: Sun Dec 07, 2008 5:03 am
by eracha
hi,
can anyone figure this thimg out for me. Its really messing me up now.I am willing to pay $20 to any one who helps figure this out for me.
thanks