doujia1679 2012-03-09 21:23
浏览 26
已采纳

PHP Cookie和动态插入问题

I'm trying set cookies for dynamic features. I've been able to get the on page code right that allows me to insert all dynamic features using a custom url string.

i.e.

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

As long as ALL variables are present in the incoming url, cookie for each variable sets fine. Upon return visits, user is shown all dynamic cookied features. Good.

Problem: If ALL the php variables are NOT present in the url, cookie doesn't set on individual basis.

i.e.

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

Here's the php code:

<?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条回答 默认 最新

  • dtu72460 2012-03-09 21:28
    关注

    change this:

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

    to this:

    if (isset($_COOKIE['RA_kw'])  
        || isset($_COOKIE['RA_survey_id']) 
        || isset($_COOKIE['RA_id']) 
        || isset($_COOKIE['RA_img'])) 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题