0

Hello i have a problem. i use session for any user that logs In my website. I use the session user id as a variable to show the first page of my site when one presses my website url (www.mywebsite.com) so, if the session user id is empty then returns the header

if(empty($sessionUid))
{
    $url=$base_url.'guest.php';
    header("location:$url");
}

But i want to use the empty session id to return the pages i want fot the guests not the ones that log in. On header.php i use the header

include_once('header.php');

if(isset($sessionUid) && $sessionUid>0 )
{
    include_once('topMenu.php');
    include_once('main.php');
    include_once('footer.php');
}
elseif(empty($sessionUid))
{
    include_once('topMenuGuest.php');
    include_once('guestMain.php');
    include_once('footerGuest.php');
}

I use that in every .php file i call, BUT as its obvious, every time i try to call a .php file it redirects back to header("location:$url"); Is it another way to call the header? Or another way to call the guest files?

2
Contributors
2
Replies
16
Views
2 Months
Discussion Span
Last Post by SimonIoa
2

Hi simon - not sure if I follow your post - and whether you need a redirection in all cases. Here's a simplified example off the top of my head that uses the index.php page to route all page requests - it's a trick that most MVCs use:

## index page ##

session_start();
require "../includes/settings.php";
require "../includes/page_manager.php";

## end on index page ##

## page_manager (../includes/page_manager.php) file ##

//header
if(!isset($_SESSION['user'])){
    $member = true;
    require '../member/header.php';
}else{
    $member = false;
    require '../guest/header.php';
}

//This can vary - depending if you have apache rewrite
$pagename = isset($_GET['page']) ? $_GET['page'] . '.php' : 'index.php';

//content
if(file_exists('../common/' . $pagename)){
    //display common contant - stuff that should be seen by all visitors
    require '../common/' . $pagename;
}elseif($member && file_exists('../member/' . $pagename)){
    //display members only content
    require '../member/' . $pagename;
}elseif($member){
    //display guests only notice - not sure if this is required though
    //or do a redirect
    require '../member/guestonly.php';
}elseif(!$member && file_exists('../guest/' . $pagename)){
    //display guest page
    require '../guest/' . $pagename;
}elseif(!$member){
    //members only notice and display login form
    //or do a redirect
    require '../member/memberonly.php';
    require '../guest/login.php';
}

//footer
require '../common/footer.php';

## end of page_manager ##

So folders:

/common/ all common "views" for content - e.g. "about" or "contact" content
/member/ all member "views" for content
/guest/ all guest "views" for content
/includes/ all non-view files, e.g. for settings and general functions

Serving different content for the same url can be a bit tricky for SEO - but there again, if you have member-only views, then spiders wouldn't see it anyway. There are many ways to skin a cat - this is just one - and yes, it is very bare-bones.

Edited by diafol

3

OK i solved it thanks diafol. In my case i had to call session_start(); your post reminded me that.

For people who will see this in the future, this post is the answer if you want to use session or not to include files accordingly....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.