2016-08-30 14 views
-1

私はクッキーを使用して作業しています。私のクライアントは、ビジターが自分のサイトにアクセスしたときに、そのサイトに以前にアクセスしたことを自動的にチェックします。以前にこのサイトにアクセスした場合、前に訪れたサイトは自動的にリダイレクトされます。前にこのサイトにアクセスしなかった場合、クッキーは保存され、将来このサイトにアクセスすると、最後に訪れたページにリダイレクトされます。たとえば、サイトには、食品、布などのカテゴリやトピックが多数あります。訪問者がClothのトピックまたはカテゴリでこのサイトを訪問すると、Cookieが保存されます。次回彼はこのサイトを訪れ、彼は過去に布カテゴリーのページの原因を自動的にリダイレクトされ、彼はそのページを訪れた。しかし今度は、フッタのオプションでクッキーを再度保存し、クリックしてクッキーを受け入れると、クッキーは更新されて保存されます。 これをローカルホストで行い、そのファイルを共有しようとしています。何が問題か、どこにあるか確認してください。ここでは、header.phpとfaceの問題でリダイレクトオプションを使用しています。 index.phpでredirectオプションを使用すると、エラーが発生します(スクリーンショット参照)。 header.phpで使用すると、すべてのページでエラーが発生します。スクリーンショットを参照してください:http://www.louisvuitton.com/私はどのように私はクッキーにユーザーベースをリダイレクトすることができます、私は多くのPHPを使用してリダイレクトに直面した

私のヘッダページ

<?php 
if(isset($_COOKIE['saveexperice'])){ 

    $link = $_COOKIE['saveexperice']; 
    header("Location: $link"); 
    exit; 
} 
else{ 
    header('Location: http://localhost/cookie'); 
    $exlink = $_SERVER['PHP_SELF']; 

    setcookie('saveexperice', $exlink, time()+60*60*24*30); 
    exit; 
} 

私のインデックスページ

<?php 
include("header.php"); 

//$page_link = $_SERVER['PHP_SELF']; 

echo "Index page"; 

//echo $page_link; 

print_r($_COOKIE); 




include("footer.php"); 

フッターのページ

<a href="clearcooke.php">Logout</a> 

別のページ:http://prntscr.com/cbkux6 あなたは、たとえば、このサイトを訪問することができます

<?php 
include("header.php"); 

print_r($_COOKIE); 



echo "Ex php page"; 


include("footer.php"); 

クッキー明確なページ

<?php 

$exlink = $_SERVER['PHP_SELF']; 

setcookie('saveexperice', $exlink, time()-60*60*24*30); 

header('location: index.php'); 

別のページ

<?php 
include("header.php"); 



print_r($_COOKIE); 



echo "CK php page"; 


include("footer.php"); 

私は私のコメントで述べたように、この完全なプロジェクトzipファイル

http://www116.zippyshare.com/d/6Gz32nO0/2541183/Coockie.zip

+0

あなたのヘッダは、それがリダイレクトし続けるので、ヘッダーがに転送されていることを一致させる方法はありません。ヘッダーにあなたがいる場所と一致する 'if'を持っていなければなりません。そしてあなたがあなたの場所にいなければ、それはあなたをリダイレクトし、正しい場所にいるならリダイレクトしません。 – Rasclatt

+0

別のページコードpastebin.com/yjFmt3egホームページpastebase.com/k7RH0JQu –

答えて

1

をダウンロードすることができ、私はそうではなかった明らかに、いずれのストップコードも持たないので、リダイレクトします。 EDIT

<?php 
// Move here 
$exlink = $_SERVER['PHP_SELF']; 

if(isset($_COOKIE['saveexperice'])){ 
    $link = $_COOKIE['saveexperice']; 
    // If your cookie doesn't match where you are now 
    if($exlink != $link) { 
     // Redirect 
     // NOTE: You may want to check the timestamp and only redirect 
     // if the cookie is X-amount of minutes old otherwise you 
     // will probably be stuck in another loop, always pushing you 
     // to the same page. 
     // If it's within the timeframe to not push to another page, 
     // then you have to reset the cookie to the current page. 
     header("Location: {$link}"); 
     exit; 
    } 
} 
else{ 
    setcookie('saveexperice', $exlink, time()+60*60*24*30); 
    // I am not sure why you need to redirect here since you are on a page 
    // you supposedly want to be on 
    header('Location: http://localhost/cookie'); 
    exit; 
} 

さてさて、あなたは私の編集は、仕事を得ることができないので、ので、私はいくつかの人間を追加するためにこれに別のレイヤーを追加しているあなたはリダイレクトしないという条件がありませんコードを理解することができます。 ITはあなたが上に構築できるクイッククラスですが、すべてのメソッドはかなり自明です。それは私が使用して1からの部品(一般に)です:

<?php 
# I am calling it Session, but that is because it would have both cookie and session methods 
class Session 
    { 
     private $expireTime, 
       $cookieName; 
     /* 
     ** @description This will set the time for the cookie to expire 
     */ 
     public function setTime($time) 
      { 
       $this->expireTime = $time; 
       return $this; 
      } 
     /* 
     ** @description Returns the name of the last cookie used in the instance 
     */ 
     public function getName() 
      { 
       return $this->cookieName; 
      } 
     /* 
     ** @description This will set the name of the cookie 
     */ 
     public function setName($name = false) 
      { 
       $this->cookieName = $name; 
       return $this; 
      } 
     /* 
     ** @description This actually creates the cookie 
     */ 
     public function setCookie($val, $name = false) 
      { 
       if(!empty($name)) 
        $this->setName($name); 

       if(empty($this->cookieName)) 
        return false; 

       $this->expireTime = (!empty($this->expireTime))? $this->expireTime : (time()+60*60*24*30); 
       setcookie($this->cookieName,json_encode(array($this->expireTime,$val)),$this->expireTime); 
      } 
     /* 
     ** @description Self-explanatory 
     */ 
     public function destroyCookie($name = false) 
      { 
       if(!empty($name)) 
        $this->setName($name); 

       if($this->cookieExists($this->cookieName)) 
        setcookie($this->cookieName,null,(time()-1000)); 
      } 
     /* 
     ** @description Self-explanatory 
     */ 
     public function cookieExists($name = false) 
      { 
       if(!empty($name)) 
        $this->setName($name); 

       return (isset($_COOKIE[$this->cookieName])); 
      } 
     /* 
     ** @description Self-explanatory 
     */ 
     public function getCookie($name = false) 
      { 
       $cookie = $this->getCookieData($name); 

       return (!empty($cookie[1]))? $cookie[1] : $cookie; 
      } 
     /* 
     ** @description This will get an array of the value and expire time 
     */ 
     public function getCookieData($name = false) 
      { 
       if(!empty($name)) 
        $this->setName($name); 

       return (!empty($_COOKIE[$this->cookieName]))? json_decode($_COOKIE[$this->cookieName],true) : false; 
      } 
     /* 
     ** @description Checks if the cookie is expired 
     */ 
     public function isExpired($name = false) 
      { 
       $cookie = $this->getCookieData($name); 
       if(!empty($cookie[0])) 
        return false; 

       return true; 
      } 
     /* 
     ** @description Gives an array for a countdown of sorts 
     */ 
     public function willExpire($name = false) 
      { 
       $cookie = $this->getCookieData($name); 
       $now = strtotime("now"); 
       if(!empty($cookie[0])) { 
        $seconds = ($now - $cookie[0]); 
        return array(
           'h'=>trim(number_format(($seconds/60/60),0),'-'), 
           'm'=>trim(number_format(($seconds/60),0),'-'), 
           's'=>trim($seconds,'-') 
          ); 
       } 

       return true; 
      } 
     /* 
     ** @description Resets the expire time on the cookie 
     */ 
     public function extendTime($time,$name=false) 
      { 
       $cookie = $this->getCookieData($name); 
       $this->setTime($time)->setCookie($cookie[1]); 
      } 
    } 

使用するには:あなたがページ上にある場合

<?php 
# Add the class 
require_once(__DIR__.'/Session.php'); 
# Create instance 
$cEngine = new Session(); 
# Check if the cookie exists already 
if(!$cEngine->cookieExists('saveexperice')) { 
    # If it doesn't exist, create it by 
    # First setting an expire time 
    $cEngine->setTime(strtotime('now + 20 minutes')) 
      # Add the data 
      ->setCookie($_SERVER['PHP_SELF'],'saveexperice'); 
} 
# This would just echo the value 
echo $cEngine->getCookie(); 
# This will tell you when it will expire (count down) 
print_r($cEngine->willExpire()); 
# This will extend the expiration time on the cookie 
$cEngine->extendTime(strtotime("now + 1 day")); 
# This will destroy the cookie 
# $cEngine->destroyCookie(); 
+0

お返事ありがとうございます。 私はlocalhostでこのコードを使用していましたが、それはうまくいきましたが、私はこのコードをウェブサイトで使用し、ウェブサイトでは使用しませんでした。クッキーは保存されません、私はボディコンテンツのエコークッキーでしたが、エコーしません。 どうすれば解決できますか?手伝って頂けますか? 別のページコード http://pastebin.com/yjFmt3eg ホームページ http://pastebin.com/k7RH0JQu –

+0

さてさて、私はいくつかの人間が読めるメソッドを持つクラスを追加しました。うまくいけば、あなたのクッキーの設定を簡単に作り出すことができます。 – Rasclatt

+0

私はこのポストに関連した必要な助けのための新しいポストを持っています、あなたは見ることができますし、あなたは私を助けることができますか? http://stackoverflow.com/questions/39256311/cookie-is-working-fine-in-root-directory-but-not-working-sub-directory –

関連する問題

 関連する問題