php redirect and login

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
eracha
Forum Newbie
Posts: 4
Joined: Thu Dec 04, 2008 8:39 am

php redirect and login

Post 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
User avatar
papa
Forum Regular
Posts: 958
Joined: Wed Aug 27, 2008 3:36 am
Location: Sweden/Sthlm

Re: php redirect and login

Post 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 :)
eracha
Forum Newbie
Posts: 4
Joined: Thu Dec 04, 2008 8:39 am

Re: php redirect and login

Post 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
eracha
Forum Newbie
Posts: 4
Joined: Thu Dec 04, 2008 8:39 am

Re: php redirect and login

Post 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.
User avatar
Syntac
Forum Contributor
Posts: 327
Joined: Sun Sep 14, 2008 7:59 pm

Re: php redirect and login

Post by Syntac »

Missing a semicolon.
eracha
Forum Newbie
Posts: 4
Joined: Thu Dec 04, 2008 8:39 am

Re: php redirect and login

Post 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
Post Reply