Forum Moderators: coopster

Message Too Old, No Replies

User sessions - how to?

         

Grenz

7:32 pm on Feb 24, 2009 (gmt 0)

10+ Year Member



Hi all
I have a site where some of the pages only should be able to acces if your logged in.
If your not logged in - the pages should still be available in the menu but if clicked it should send the user to a site called something like "you have to log in to view this site - log in here..."

So far I have this:

<?php
if(Session::isLoggedIn()){
?>
IF LOGGED IN - THE USER SHOULD BE REDIRECTED TO THE WANTED SITE (1)
<?
}
else{
echo "IF NOT LOGGED IN THE USER SHOULD BE REDIRECTED TO ANOTHER SITE";
}
?>

How should i make the redirections?
Or is there a better way to achieve the same effect?

(1) fx. if the user clicks on "menuitem2" the user should be sent to "menuitem2.htm" if logged in. Otherwise the user should be sent to "pleaselogin.htm"

Thanks in advance :-)

enigma1

10:10 am on Feb 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could do the redirections using the php header function. So you can setup a redirect function like:

function page_redirection($url, $type_redirect='301') {
// Do not redirect if headers already sent
if( headers_sent() ) {
// Also you may need to close session
// session_write_close();
exit();
}
header("HTTP/1.1 " . $type_redirect);
header('Location: ' . $url);
// Also you may need to close session before exiting
// session_write_close();
exit();
}

So then you can call the function with a URL for each case you mentioned.
You could try passing the urls in it.
1. [www1.example.com...]
2. [www2.example.com...]

Grenz

11:30 am on Feb 25, 2009 (gmt 0)

10+ Year Member



Hi
Thanks for the reply.
I actually solved myself in a rather simple but effective way:

<?php
If ($_SESSION['example_sessioncompany'])
{
}
else{
header("Location: http://example.com/login");

}
?>

I use this snippet in all my "you have to be logged in to veiw" pages.

[edited by: eelixduppy at 7:34 pm (utc) on Feb. 25, 2009]