PHP Cookie和动态插入问题

时间:2012-03-09 21:23:52

标签: php cookies

我正在尝试为动态功能设置Cookie。我已经能够获得正确的页面代码,允许我使用自定义url字符串插入所有动态功能。

http://mysite.com/dynamicpage.php?RA_kw=Keyword-
keyword&RA_survey_id=survey_id&RA_id=123&RA_img=imgname

只要传入的URL中存在所有变量,每个变量的cookie就可以设置好。返回访问后,将向用户显示所有动态烹饪功能。好。

问题:如果网址中没有所有php变量,则不会单独设置Cookie。

http://mysite.com/dynamicpage.php?RA_kw=Keyword-keyword
http://mysite.com/dynamicpage.php?RA_id=123 

这是php代码:

<?php 
/*kw = ( Keywords) 
survey_id=survey_id (this variable doesn't change) 
id= ( survey number id) 
img = ( name of image to be pulled from php include.)*/

$kw = null; 
$survey_id = null; 
$id = null; 
$img = null; 

if (isset($_COOKIE['RA_kw'])  
    && isset($_COOKIE['RA_survey_id']) 
    && isset($_COOKIE['RA_id']) 
    && isset($_COOKIE['RA_img'])) 
{ 
    //if cookie variables are already set 

    //To Do Here:  maybe redirect 
    $kw = $_COOKIE['RA_kw']; 
    $survey_id = $_COOKIE['RA_survey_id']; 
    $id = $_COOKIE['RA_id']; 
    $img = $_COOKIE['RA_img']; 
    $_GET['RA_kw'] = $kw; 
    $_GET['RA_survey_id'] = $survey_id; 
    $_GET['RA_id'] = $id; 
    $_GET['RA_img'] = $img; 
} 
else 
{ 
    //if cookie varialbes are not set yet 

    //set Cookies 
    if (isset($_GET['RA_kw'])){ 
        //kw parameter is set 
        setcookie('RA_kw', $_GET['RA_kw'], time() + 60*60*24*30);    //expires in 30 days. 
        $kw = $_GET['RA_kw']; 
    } 
    if (isset($_GET['RA_survey_id'])){ 
        //survey_id parameter is set 
        setcookie('RA_survey_id', $_GET['RA_survey_id'], time() + 60*60*24*30);    //expires in 30 days. 
        $survey_id = $_GET['RA_survey_id']; 
    } 
    if (isset($_GET['RA_id'])){ 
        //id parameter is set 
        setcookie('RA_id', $_GET['RA_id'], time() + 60*60*24*30);    //expires in 30 days. 
        $id = $_GET['RA_id']; 
    } 
    if (isset($_GET['RA_img'])){ 
        //img parameter is set 
        setcookie('RA_img', $_GET['RA_img'], time() + 60*60*24*30);    //expires in 30 days. 
        $img = $_GET['RA_img']; 
    } 

    //To Do Here: default page 
} 
?>

1 个答案:

答案 0 :(得分:2)

改变这个:

if (isset($_COOKIE['RA_kw'])  
    && isset($_COOKIE['RA_survey_id']) 
    && isset($_COOKIE['RA_id']) 
    && isset($_COOKIE['RA_img'])) 

到此:

if (isset($_COOKIE['RA_kw'])  
    || isset($_COOKIE['RA_survey_id']) 
    || isset($_COOKIE['RA_id']) 
    || isset($_COOKIE['RA_img']))