2017-07-28 136 views
-1

我正在尝试使用$ _GET vars(params)创建一个动态页面,并且如果var等于某些内容,它就会工作。但是,如果var不等于某个东西,那么它或者显示内容,或者不显示错误;或显示错误,并在同一时间内容

<?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 
} 

>

回答

0

你在你的代码的两个问题:?

  1. 您的错误检查需要使用&&而不是|| 。你想看看loginregister都没有使用。
  2. 您需要使用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 
} 
0

问题是因为您使用的是比较运营商在支票发生:

if($type != 'login' || $type != 'register')

从本质上讲,这是两个独立的条件;当页面不是login时,第一个将评估为true,当页面不是register时,第二个将评估为true但是,他们没有检查彼此

第一个条件将认为register有效,因为register对于if($type != 'login')有效。第二个将认为login有效,因为login对于if($type != 'register')有效。

你需要确保这些网页的既不是允许的,通过使用比较(&&):if($type != 'login' && $type != 'register')

然而,对于这件事,你不必担心设置$type可变。简单地检查什么_GET['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"); 
} 

而且,请注意,mysql_构造是deprecated as of PHP 5.5,而且是彻底的removed in PHP 7。请考虑切换到MySQLiPDO,确保您也使用parameterised queries来防止SQL injection

希望这会有所帮助! :)

0

你只需要制定更多的逻辑......如果,否则如果,其他。案件处理是问题。

<?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'])))); 
    } 
} 
...... 
?> 

对不起,一半烤答案,我我的手机上,它不是代码非常友好