-2

我无法在 switch/case 条件下为 $_SESSION['next'] 设置会话,而 $_SESSION['user_id'] 在条件之前完美工作。脚本运行到 switch/case 条件的每个条件并重定向而不设置 $_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;
}
4

2 回答 2

3

你的开关全错了。阅读手册并尝试以下操作:

<?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情况。这将导致您遇到的行为。

于 2012-08-20T18:00:13.967 回答
0

I suggest:

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

(parentheses, and the != operator)

and also a DRYer switch:

$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();
于 2012-08-20T18:13:27.093 回答