Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP设置cookie的问题_Php_Cookies - Fatal编程技术网

使用PHP设置cookie的问题

使用PHP设置cookie的问题,php,cookies,Php,Cookies,我通常不这么做,但我在这里完全没有希望了 我试图创建一个简单的系统来跟踪从广告到登录页的线索。为此,我想保存客户端访问的页面的cookie,但由于某些原因,我无法设置cookie,即使setcookie()函数返回true 我很确定我在发送任何标题之前设置了cookie,我还检查了firefox、chrome和explorer上的问题,结果是一样的 提前感谢任何愿意帮助的人 <?php //save the received user information to local varia

我通常不这么做,但我在这里完全没有希望了

我试图创建一个简单的系统来跟踪从广告到登录页的线索。为此,我想保存客户端访问的页面的cookie,但由于某些原因,我无法设置cookie,即使setcookie()函数返回true

我很确定我在发送任何标题之前设置了cookie,我还检查了firefox、chrome和explorer上的问题,结果是一样的

提前感谢任何愿意帮助的人

 <?php
//save the received user information to local variables:
$cid = $_GET['campaignId'];
$redirectURL = $_GET['url'];
//save the click in a database:
$servername = *********
$username = ******
$password = ******
$dbname = *******
//check by cookie if its the first visit, if it is set cookie to the current cid (campaign id).
$cookieCounter=0;
$first=true;
if(isset($_cookie['cid']))
{
    foreach($_cookie['cid'] as $cookieCid)
    {
        if($cookieCid == $cid)
        {
            $first=false;//returning visitor
            $cookieCounter++;//used for cookie array reference later on.
            break;
        }
    }
}
   if($first)
    {
      /*if its a first time visit to the current campaign:
create a new cookie log
update DB*/
    //cookie update:
    if(setcookie("cid[$cookieCounter]",$cid,time()+60*60*24*30*2))
  //set expiration date for 2 months
    //database UPDATE
    // Create connection
    $conn = new mysqli($servername, $username, $password,$dbname);
    // Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

//update user visit:

$sql = "UPDATE campaign SET uniqueVisitors = uniqueVisitors + 1 WHERE cid = $cid";
$result = $conn->query($sql);

if ($result) 
{
echo "<script>window.alert(\"Success! Update Unique Visitors\")</script>";
}
else 
{
    echo "<script>alert(\"Sorry, something went wrong :( could not update unique visitors ".mysqli_connect_error()."end of report\")</script>";
}
$conn->close();
}
    // Create connection
    $conn = new mysqli($servername, $username, $password,$dbname);
     // Check connection
   if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    //update user visit:
    $sql = "UPDATE campaign SET visitors = visitors + 1 WHERE cid = $cid";
        $result = $conn->query($sql);
        if ($result) 
        {
          //echo "<script>window.alert(\"Success!\")</script>";
        }
        else 
        {
           echo "<script>alert(\"Sorry, something went wrong :( ".mysqli_connect_error()."end of report\")</script>";
        }
       $conn->close();
        echo $cid."   ".$redirectURL;
?>


您应该使用$\u COOKIE['cid']而不是$\u COOKIE['cid']。

您应该使用$\u COOKIE['cid']而不是$\u COOKIE['cid']。

我更改了它,但我仍然有问题。但您的问题可能是另一个问题吗?我尝试了你的脚本,得到了我推荐的修改后的cookie。但是如果你的意思是它没有将更新的访问者保存在数据库中,那么还有一个问题与cookie有关。非常感谢!它确实修好了!谁知道问题这么简单!再次感谢你把我从几个小时的挫折中解救出来!我的建议可以帮助你,你可以接受我的回答和评价。谢谢。我改了,但我还是有问题。但你的问题可能是另一个问题吗?我尝试了你的脚本,得到了我推荐的修改后的cookie。但是如果你的意思是它没有将更新的访问者保存在数据库中,那么还有一个问题与cookie有关。非常感谢!它确实修好了!谁知道问题这么简单!再次感谢你把我从几个小时的挫折中解救出来!我的建议可以帮助你,你可以接受我的回答和评价。非常感谢。