我的PHP代码有点问题,我根据收到的内容将值分配给流的不同状态的变量,但由于某种原因,它一直卡在一点,这是代码.
if (isset($session)) {
//if the user is in the database
if ($row == 1) {
$from = $_GET['from'];
if (isset($from)) {
$page = $_GET['page'];
switch ($page) {
case "game":
$page = "game";
sendVars($page);//send the variable
break;
case "gallery":
$page = "gallery";
sendVars($page);//send the variable
break;
case "leaderboard":
$page = "leaderboard";
sendVars($page);//send the Variable
break;
}
}else {
$page = "game";
sendVars($page);//send the variable
}
//if the user is not in the database
}else {
//do this
}
} else {
//register
}
Run Code Online (Sandbox Code Playgroud)
现在由于一些奇怪的原因,它一直将$ page的值设置为游戏,即使我将页面变量设置为图库,如http://www.mydomai.com/?from=set&page=gallery.我能想到的唯一原因是我的开关不能正常工作吗?或者它以某种方式绕过开关?
Thanx提前!
我删除了一些无用的变量赋值后才运行你的代码:
<?php
// I added this function just for testing
function sendVars($page) {
echo $page;
}
if (isset($_GET['from'])) {
$page = $_GET['page'];
switch ($page) {
case "game":
sendVars($page); //send the variable
break;
case "gallery":
sendVars($page); //send the variable
break;
case "leaderboard":
sendVars($page); //send the Variable
break;
}
} else {
$page = "game";
sendVars($page); //send the variable
}
Run Code Online (Sandbox Code Playgroud)
这一切看起来都很好,xxx.php?from = 1&page = gallery echos out"gallery",尝试在脚本顶部执行print_r($ _ GET)并查看打印出来的内容并告诉我们.
另外,我认为以下内容对您来说可能更短,但仍然做同样的事情:
if (isset($_GET['from'])) {
// Check if $_GET['page'] exsists and is either game, gallery or leaderboard
if (isset($_GET['page']) && in_array($_GET['page'], array('game', 'gallery', 'leaderboard')))
sendVars($_GET['page']);
}
else
sendVars('game');
Run Code Online (Sandbox Code Playgroud)
我希望这有帮助
干杯卢克