无法在交换机/案例条件下设置会话

时间:2012-08-20 17:57:15

标签: php switch-statement

我无法在切换/案例条件下为$ _SESSION ['next']设置会话,而$ _SESSION ['user_id']在条件之前完美地工作。该脚本会遇到切换/案例条件的每个条件并重定向而不设置$ _SESSION ['next']。是否有任何具体原因导致其失效?怎么解决这个问题?

require_once ('../src/facebook.php');
require_once ('../src/fbconfig.php');

//Facebook Authentication part
$user_id = $facebook->getUser();

if ($user_id <> '0' && $user_id <> '') {
session_start();
$_SESSION['user_id'] = $user_id;

switch((isset($_GET['page']) ? $_GET['page'] : '')){
    case 'abc';{
        $_SESSION['next'] = 'AAA';
        echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
        exit;}  
    case 'def';{
        $_SESSION['next'] = 'BBB'; 
        echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
        exit;}  
    case 'ghi';{
        $_SESSION['next'] = 'CCC'; 
        echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
        exit;}
    default;{
        echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
        exit;}                                                                                                                                                                              
}   

} else {
echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";   
exit;
}

2 个答案:

答案 0 :(得分:3)

你的开关都错了。 Read the manual并试一试:

<?php

switch ((isset($_GET['page']) ? $_GET['page'] : '')){
    case 'abc':
        $_SESSION['next'] = 'AAA';
        echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
        break;
    case 'def':
        $_SESSION['next'] = 'BBB';
        echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
        break;
    case 'ghi':
        $_SESSION['next'] = 'CCC';
        echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
        break;
    default:
        echo "<script>top.location.href = 'https://www.example.com/xxx/'</script>";
        break;
}

你在exit中使用switch,除非你希望你的脚本在交换机上结束,否则这是一个禁忌。相反,您必须使用break关键字。

您还可以为每种情况使用分号和花括号。

case 'ghi';{ ... }

不! 正确使用

case 'ghi':
    .
    .
    .
    break;

更新:我刚注意到你使用这一行:

if ($user_id <> '0' && $user_id <> '') { ... }

<>在PHP代码中做什么? “不等于”的“标准”运算符是PHP中的!=。正确使用它或没有人想要使用您的代码。

第二次更新:您永远不会在$_SESSION['next']案例中设置default。您的switch很可能始终转向default案例。这会导致您遇到的行为。

答案 1 :(得分:0)

我建议:

if (($user_id != '0') && ($user_id != ''))

(括号和!=运算符)

以及DRYer开关:

$page = array_key_exists('page', $_GET) ? $_GET['page'] : '';

switch ($page) {
    case 'abc':
        $next = 'AAA';
        $loc  = 'https://www.example.com/xxx/';
        break;
    case 'def':
        $next = 'BBB';
        $loc  = 'https://www.example.com/yyy/';
        break;
    ... // and so on
}
if (isset($next)) {
    $_SESSION['next'] = $next;
    // If it does not work, you have problems with your session ID, maybe?
}

// I find this syntax easier
print <<<SCRIPT

<script type="text/javascript">
    top.location.href = '$loc';
</script>

SCRIPT;
exit();