Php 如何判断$\u GET参数是否等于
我试图用$_getvars(params)创建一个动态页面,如果var等于某个值,它将显示内容。但是如果var不等于某个值,那么它要么仍然显示内容,要么不显示错误;或同时显示错误和内容Php 如何判断$\u GET参数是否等于,php,html,Php,Html,我试图用$_getvars(params)创建一个动态页面,如果var等于某个值,它将显示内容。但是如果var不等于某个值,那么它要么仍然显示内容,要么不显示错误;或同时显示错误和内容 <?php if(!isset($_GET['type'])){ header("location: ?type=login"); } else { $type = trim(strip_tags(stripslashes(mysql_real_escape_string($_GET['ty
<?php
if(!isset($_GET['type'])){
header("location: ?type=login");
} else {
$type = trim(strip_tags(stripslashes(mysql_real_escape_string($_GET['type']))));
}
if($type != 'login' || $type != 'register'){
?>
<h1>What your looking for can't be found!</h1>
<?php
}
if($type == 'login'){
?>
<h1>Login page:</h1>
<?php
}
if($type == 'register'){
?>
<h1>Register page:</h1>
<?php
}
你要找的东西找不到!
登录页面:
注册页面:
您的代码中有两个问题:
检查错误时需要使用&&
,而不是|
。您想查看login
和register
是否都未使用
您需要使用if/else if
语句来确保只存在一个条件
请查看以下代码:
<?php
if(!isset($_GET['type'])){
header("location: ?type=login");
} else {
$type = trim(strip_tags(stripslashes(mysql_real_escape_string($_GET['type']))));
}
if($type != 'login' && $type != 'register'){
?>
<h1>What your looking for can't be found!</h1>
<?php
} else if($type == 'login'){
?>
<h1>Login page:</h1>
<?php
} else if($type == 'register'){
?>
<h1>Register page:</h1>
<?php
}
你要找的东西找不到!
登录页面:
注册页面:
出现此问题的原因是您在检查中使用了一个比较运算符:
if($type!='login'| |$type!='register')
本质上,这是两个不同的条件;当页面未登录时,第一个将计算为true
,当页面未注册时,第二个将计算为true
但是,他们没有相互检查
第一个条件将考虑<代码>寄存器>代码>有效,如<代码>登记器< /> >对于<代码>有效(如果$type!=登记)。第二种方法认为登录>代码>有效,如<代码>登录<代码>对<代码>有效(如果$type!=登记)< <代码> > /p>
您需要使用和比较器(&&
):如果($type!='login'&&&$type!='register'),确保这些页面都不允许
但是,对于这个问题,您根本不需要担心设置$type
变量。只需在检查设置的同时检查“获取['type']
等于什么:
<?php
if (isset($_GET['type']) && $_GET['type'] == 'login') {
?>
<h1>Login page:</h1>
<?php
}
else if (isset($_GET['type']) && $_GET['type'] == 'register') {
?>
<h1>Register page:</h1>
<?php
}
else {
header("location: ?type=login");
}
登录页面:
注册页面:
你只需要把逻辑再弄清楚一点…如果,否则如果,否则。案件处理是问题所在
<?php
if(!isset($_GET['type'])){
header("location: ?type=login");
} else if(isset($_GET['type'] {
If ($_GET['type'] !== "") {
$type = trim(strip_tags(stripslashes(mysql_real_escape_string($_GET['type']))));
}
}
......
?>