Friday, 30 August 2013

Set cookie fails for first time but works on refresh

Set cookie fails for first time but works on refresh

Though after reading explanations about setting cookie and not working for
first time i find it difficult to resolve the below problem as am new to
php and cookies.
I have a webpage with for (e.g) cp.php, login.php, header.php,
maindata.php , bottom.php. Whenever i login to the webpage cp.php will be
processed from there 1.header.php will be called first 2.maindata.php will
be called and 3.bottom.php will be called.
So am setting my cookie at maindata.php and the code is like,
<?php
$cid = $_GET["id"];
$XmlPath = $_GET["path"];
$numpath = $_GET["numpath"];
$finepath =$_GET["finepath"];
$Tech =$_GET["tech"];
$read_str="";
function read($Path)
{
$temp="";
if(file_exists($Path))
{
$library = new SimpleXMLElement($Path,null,true);
foreach($library->children("SAS") as $info){
foreach($info->children("SAS") as
$attributes){
$nameVal =
$attributes->Name."=".$attributes->Value;
$str_temp .=$nameVal."#";
}
}
}else
{
$str_temp ="NA";
}
return $str_temp;
}
$arrpath =explode(",",$XmlPath);
/*Reading and storing arrpath[0] has the path of xml to be parsed*/
$strG=read($arrpath[0]);
$strC=read($arrpath[1]);
$strB =read($arrpath[2]);
setcookie($cid.'strE',$strG);
setcookie($cid.'comstr',$strC);
setcookie($cid.'basstr',$strB);
(....)
in the same file am reading the cookie using the below code,
$read_str
=$_COOKIE[$cid.'strE'].$_COOKIE[$cid.'comstr'].$_COOKIE[$cid.'basstr'];
after this process is done bottom.php will be called and for the first
time loading is completed.As i said for the first time am not getting any
value in $read_str, but if i refresh the page and do all the process again
i am getting the value.
As SETCOOKIE will return TRUE incase of successfully setting cookie i
tried putting it in an if-loop and it returned false even for the first
time.
kindly assist me in finding where the problem exists!

No comments:

Post a Comment