Stephen Cioffi
Stephen Cioffi

Reputation: 1221

PHP session issues

I have a mobile script from detectmobilebrowsers.com that will redirect the user to my mobile site however I also wish that when the URL "http://example.com/?mobile=no" is entered a session will be created that won't redirect the user on every page of my site...

$mobile=$_GET['mobile'];

if(isset($_SESSION['mobile'])){
    if($_SESSION['mobile']==="no"){
        complete();
    }
    else{
        $_SESSION['mobile']="no";
        complete();
    }
}
elseif($mobile==="no"){
    $_SESSION['mobile']="no";
    complete();
}
elseif($_SESSION['mobile']!="no"){
    checkMobile();
}
function checkMobile(){
    // Mobile Detection Code taken out to save space.
    gotoMobile();
}
function gotoMobile(){
    echo "<script>window.location='http://m.MySite.org/';</script>";
}
function complete(){
    return false;
}

Sorry if I seem confusing but in short terms: Mobile Detection (which is set)... make session mobile=no if user does wishes to view full site and when that session is created it is checked on everypage (same php script) and if I set my session for no mobile I want that to stay on everypage... In my case the only thing that happens is the first page is not redirected but when I go to another page it won't display it unless I add the ?mobile=no but the whole point of the sessions here is so this only needs to be done once.

Upvotes: 0

Views: 242

Answers (2)

calebds
calebds

Reputation: 26228

Before you can begin storing user information in your PHP session, you must first start the session:

session_start();

There must be no markup ouputted before session_start(), not even whitespace! (unless output buffering is used).

See http://php.net/manual/en/function.session-start.php.

Upvotes: 1

jjs9534
jjs9534

Reputation: 475

It sounds simple, but are you sure you are using session_start() at the top of every page before checking all of your session variables?

Upvotes: 1

Related Questions