2015-04-05 22 views
0

如果cookie不等於可用的,我正在努力重定向用戶。如果它等於可放大的話,那麼它應該繼續劇本。這裏是我的代碼重定向:如何重定向如果Cookie不等於可以

if(empty($_GET)) { 
    //No variables are specified in the URL. 
    //Do stuff accordingly 
    echo "No variables specified in URL..."; 
} else { 
    //Variables are present. Do stuff: 
    $id = htmlspecialchars($_GET["id"]); 
    echo 'url query is ' . $id; 
} 

if(isset($_COOKIE['logged_in']) == $id) 
{ 
    header("Location: test.php"); 
} 
if(isset($_COOKIE['logged_in']) != $id) 
{ 
    //continues the script 

請注意,在如果statment($ ID)的vairable是從URL的查詢vairable;例如,如果url是「random.com/test.php?id=17」,並且cookie等於18,則腳本應該重定向。但是,如果url是「random.com/test.php?id=17」且cookie等於17,則保持在同一頁面上。對不起,如果它聽起來compcompated。

它不工作,因爲這段代碼:它不會重定向,無論可比的等價。謝謝

+0

你能解釋一下嗎? '「random.com/test.php?id=17」和cookie等於18腳本應重定向'當腳本說如果相等,它應該重定向。你能回顧你的問題嗎? – 2015-04-05 09:19:43

回答

-1

你應該添加另一個條件。

if(empty($_GET)) { 
    //No variables are specified in the URL. 
    //Do stuff accordingly 
    echo "No variables specified in URL..."; 

} else { 
    //Variables are present. Do stuff: 
    $id = htmlspecialchars($_GET["id"]); 
    echo 'url query is ' . $id; 
} 

if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id) 
{ 
    header("Location: test.php"); 
} 
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] != $id) 
{ 
    //continues the script 

或使用該腳本

if(isset($_COOKIE['logged_in'])) 
{ 
    if($_COOKIE['logged_in']==$id){ 
     header("Location: test.php"); 
    } 
    else{ 
     //another condition to equal is not equal so directly we can use else 
     //continues the script 
    } 
} else { 
echo "Cookie not valid or available"; 
// redirect user 
} 
1

你在找這樣的事情。如果是這樣,它應該適用於您的情況:

<?php 
if(empty($_GET)) { 
    //No variables are specified in the URL. 
    //Do stuff accordingly 
    echo "No variables specified in URL..."; 
} else { 
    //Variables are present. Do stuff: 
    $id = htmlspecialchars($_GET["id"]); 
    echo 'url query is ' . $id; 
} 

if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id) 
{ 
    header("Location: test.php"); 
} 
if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']!=$id) 
{ 
    //continues the script 
} 
?> 
0

只有在發送給客戶端後纔會應用標頭。如果你想立即重定向,你可以在header(...)之後放置exit(0),這樣你將停止執行腳本,並將當前標題發送到瀏覽器,然後重定向你。

if(isset($_COOKIE['logged_in']) && $_COOKIE['logged_in']==$id) { 
    header("Location: test.php"); 
    exit(0); 
} 

//continues the script 
0

的問題是,你與值的GET參數isset(結果)的 「」 相比,$id

if(isset($_COOKIE['logged_in']) == $id) 

它說的是「確定是否設置了$_COOKIE['logged_in']並將該確定與$id「進行比較」。 PHP將評估isset,返回truefalse(因爲它在documentation說),和比較truefalse表達式(==)的另一邊,這意味着$id,它永遠不會匹配給你的例子。如果您查詢「random.com/test.php?id=true」(或false)可能會做你正在尋找的東西。

你行不意味着「確定$_COOKIE['logged_in']設置和$_COOKIE['logged_in']值進行比較的價值$id」,我相信這是你在找什麼。在這種情況下,你想要做的是首先檢查$_COOKIE['logged_in']設置然後檢查$_COOKIE['logged_in']比賽$id,就像這樣:

if (isset($_COOKIE['logged_in']) && $_COOKIE['logged_in'] == $id) 

如果這沒有意義,在這裏是一個非常明確的版本,可能會更清楚實際發生的事情:

if ((isset($_COOKIE['logged_in']) == true) && ($_COOKIE['logged_in'] == $id)) 

希望有所幫助。

相關問題