2017-07-28 138 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'])))); 
    } 
} 
...... 
?> 

對不起,一半烤答案,我我的手機上,它不是代碼非常友好