2012-09-17 62 views
0

我有下面的代碼PHP SESSION和COOKIE

if ($does_user == 1)//Als het aantal rijen gelijk is aan 1, zorg wel met registreren dat er geen dubbele gebruikers in de database kunnen worden toegevoegd 
     { 
      //Extract de array naar strings 
      extract($row); 

      if ($acces_level == 1) 
      { 
       if (isset($_POST["remember"])) 
       { 
        //Cookie aanmaken met de id van de gene die inlogt 
        setcookie("user_id", $user_id, time()+60*60*24*100, "/"); 

        //Naar de profiel pagina 
        header("location: profile.php?user_id=".$user_id.""); 
       } 
       else 
       { 
        //Session aanmaken met de id van de gene die inlogt 
        $_SESSION["user_id"] = $user_id; 

        //Naar de profiel pagina 
        header("location: profile.php?user_id=".$user_id.""); 
       } 
      } 
      elseif ($acces_level == 2) 
      { 
       if (isset($_POST["remember"])) 
       { 
        //Cookie aanmaken met de id van de gene die inlogt 
        setcookie("user_id", $user_id, time()+60*60*24*100, "/"); 

        //Naar de admin pagina 
        header("location: admin/index.php?user_id=".$user_id.""); 
       } 
       else 
       { 
        //Session aanmaken met de id van de gene die inlogt 
        $_SESSION["user_id"] = $user_id; 

        //Naar de admin pagina 
        header("location: admin/index.php?user_id=".$user_id.""); 
       } 
      } 
     } 

一個登錄表單,當它轉到profile.php頁面發送一個ID與URL。所以我建立一個函數,當用戶在URL中更改它的ID時,我將頭部變爲404.php 但是這個函數運行不正常,我不知道爲什麼。

function user_exists() 
{ 
    if (isset($_SESSION["user_id"]) !== $_GET["user_id"]) 
    { 
     header("location: 404.php"); 
     exit(); 
    } 
    elseif (isset($_COOKIE["user_id"]) !== $_GET["user_id"]) 
    { 
     header("location: 404.php"); 
     exit(); 
    }   
} 

的$ _GET [「user_ID的」]等於會話或Cookie,但它無論如何朝向對404.php,可有人向我解釋,爲什麼?

+2

isset()函數返回true或false,你是一個布爾值比較字符串(這應該alawys是假的) –

+0

@GeraldSchneider我無法相信我錯過了。彈出成答案,你有我的+1馬上:) – Fluffeh

+0

啊,這是我的錯誤,我想我可以比較這些值。所以首先檢查它的設置,然後比較Thhx的快速答案 – Julez

回答

2

這應該工作打算:

function user_exists() 
{ 
    if (isset($_SESSION["user_id"]) && ($_SESSION["user_id"] !== $_GET["user_id"]) 
    { 
     header("location: 404.php"); 
     exit(); 
    } 
    elseif (isset($_COOKIE["user_id"]) && ($_COOKIE["user_id"]) !== $_GET["user_id"]) 
    { 
     header("location: 404.php"); 
     exit(); 
    }   
} 

首先檢查是否存儲的變量是空的,比較其與內容$ _GET變量。

+0

Thhx這工作 – Julez

0

您使用的是等於同類型比較和對比。

GET數據總是發佈爲一個字符串(這就是它是如何工作的),所以你可能在檢查對一個字符串一個int。切換到!=超過!==可能會做的伎倆你,或者你在比較測試它在自己隱蔽前,你可以做一些奇特的類型戲法。

0

檢查,如果它被設置,然後比較...

if (isset($_SESSION["user_id"]) && ($_SESSION["user_id"] !== $_GET["user_id"])){...