Skip to content Skip to sidebar Skip to footer

Putting A Session In A If And Else Statement

I've got a problem with putting a session to store some page info into a variable heres the code:

Solution 1:

session must be started on the top, and sometimes you deal with == 1 and other with $t == "1"

try this code:

// first line
session_start();

$t = $_GET['nm'];
if ($t == 1) { // use 1 instead of "1"// store session data$_SESSION['nm'] = 1;
} else {
    ?>
    <script>
        if (/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)) {
            window.location = "http://www.gouda-info.nl/mobile";
        }
    </script>
    <?php
}

$session = $_SESSION['nm'];
if ($session == 1) { // use 1 instead of "1"

}

Solution 2:

You are using js code in php, but your js will be run after entire php file executed. So use php instead;

<?php
session_start();
$t = $_GET['nm'];
if ($t == "1") {
    // store session data$_SESSION['nm'] = "1";
} else {
  if(isMobile()) {
    header('Location: http://www.gouda-info.nl/mobile');
    exit();
  }
}

$session = $_SESSION['nm'];
if ($session == "1") {
    ......
}

functionisMobile($user_agent=NULL) {
    if(!isset($user_agent)) {
        $user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
    }
    return (strpos($user_agent, 'Android') !== FALSE
            || strpos($user_agent, 'webOS') !== FALSE
            || strpos($user_agent, 'iPhone') !== FALSE
            || strpos($user_agent, 'iPad') !== FALSE
            || strpos($user_agent, 'iPod') !== FALSE
            || strpos($user_agent, 'BlackBerry') !== FALSE);
}

Post a Comment for "Putting A Session In A If And Else Statement"