将会话放在if和else语句中 [英] Putting a session in a if and else statement

查看:94
本文介绍了将会话放在if和else语句中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个将会话存储到变量
的代码:

 <?php 

$ t = $ _GET ['nm'];
if($ t ==1)
{

session_start();
//存储会话数据
$ _SESSION ['nm'] = 1;
}
else
{
?>
< script>
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location =http://www.gouda-info.nl/移动;
}
< / script>
<?php
}

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

当我尝试使用它不起作用的脚本。我使用这个脚本来重定向移动用户,但是如果他们选择使用桌面版本,他们将被允许通过激活存储的会话,如果用户通过在链接中不加任何东西或1来激活桌面版本,如下所示: / p>

  http://www.example.com/index.php?nm=1 

希望任何人都能提出明智的解决方案。 :)

编辑:

它只是失败,如果我尝试运行此代码,它给了我一个空白页面。

解决方案

会话必须在顶部开始,有时您会处理 == 1 和其他 $ t ==1



请尝试以下代码:

  //第一行
session_start();

$ t = $ _GET ['nm'];
if($ t == 1){//使用1而不是1
//存储会话数据
$ _SESSION ['nm'] = 1;
} else {
?>
< script>
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry/i.test(navigator.userAgent)){
window.location =http://www.gouda-info.nl/移动;
}
< / script>
<?php
}

$ session = $ _SESSION ['nm'];
if($ session == 1){//使用1而不是1

}


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

<?php

$t = $_GET['nm'];
if ($t=="1")
  {

    session_start();
        // 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")
  {

When i try to use the script it just doesn't work. I use this script to redirect mobile users, but if they choose to use the Desktop version they'll be allowed by activating the session that stores if the user has activated the desktop version by putting nothing or a 1 in the link like so:

http://www.example.com/index.php?nm=1

hope anyone comes up with a bright solution. :)

EDIT:

it just fails if i try to run this code, it gives me a blank page.

解决方案

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"

}

这篇关于将会话放在if和else语句中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆