I am hoping someone can help me! I have outsourced a website and have only a limited knowledge of programming... :o I want to share information between "partner" websites using cookies. Someone gave me a technical approach to give to the folks I outsourced to, but they have now confessed they cannot make it work. It is my understanding that plenty of people have indeed made this work, and so I thought I would make a posting here to see what you guys/gals thought.
Background
I want to be able to do the following between two websites, Website A and Website B:
Where I am right now
They have set it up so that Website B calls a "pass.js" file on Website A, which in turn calls a "passValue.php" page on Website A. The passValue is then supposed to read the cookie, retrieve information from the database, and then print the information back on Website B by calling a "replaceval.js" file, which replaces the value of the html on Website B.
When "passValue.php" is called from Website B, it is NOT able to read the cookie. (Note: It successfully executes the "passValue.php" file, but skips to the end else statement where they set the value to "Cannot Read the Cookie Value. Please check your Browser Settings". It does however successfully replace the text on Website B with the text from Website A, which unfortunately is the "Cannot Read..." text)
Code Fragments
Website B Call to Website A:
[*]<div id="cookieval">value</div><script src="http://www.domain.com/pass.js"></script>
Website A pass.js call to Website A passValue.php:
[*]// JavaScript Document document.write("<script type=\"text/javascript\" src=\"http://www.domain.com/passvalue.php\"></script>");//Here the the page redirected to hitlog page with values.// JavaScript Document
Website A passvalue.php (which includes call to Website A replaceval.js):
<?
include("includes/db.php");
session_start();
//global $HTTP_COOKIE_VARS, $HTTP_GET_VARS;
//setcookie('newcookies', "NREONE", 0, '/', '');
//$HTTP_COOKIE_VARS['newcookies'] = "NREONE";
//echo $_COOKIE["newcookies"];
$ckval = "";
$new = "";
$new = $_COOKIE["newcookies"];
if($new != "")
{
$result = mysql_fetch_object(mysql_query("select * from lty_userregistration where cookieid='$new'")) or die(mysql_error());
$ckval1 = $result->userid;
$logname = $result->email;
//..............................................................
$flag1 = "b";
$flag2 = "t";
$sqlt = "select * from lty_preference where userid='$ckval1' and tbflag= '$flag2' order by subcatid";
$rest = mysql_query($sqlt) or die (mysql_error());
if(mysql_num_rows($rest)>0)
{
$i = 0;
$topsub = array();
$topcat = array();
while($rowst= mysql_fetch_object($rest))
{
$subcatid = $rowst->subcatid;
$catid = $rowst->catid;
$sqlsub = "select * from lty_subcategory where subcatid='$subcatid' ";
$ressub = mysql_query($sqlsub) or die (mysql_error());
$show = mysql_fetch_object($ressub);
$subname2 .= $show->subcatname.",";
$tyu = "select * from lty_category where catid='$catid'";
$uti = mysql_query($tyu) or die (mysql_error());
$view = mysql_fetch_object($uti);
$catname = $view->catname;
$topsub[$i] = $subname;
$topcat[$i] = $catname;
$i++;
}
}
$sqlt = "select * from lty_preference where userid='$ckval1' and tbflag= '$flag1' order by subcatid";
$rest = mysql_query($sqlt) or die (mysql_error());
if(mysql_num_rows($rest)>0)
{
$i = 0;
$bottomsub = array();
$bottomcat = array();
while($rowst= mysql_fetch_object($rest))
{
$subcatid = $rowst->subcatid;
$catid = $rowst->catid;
$sqlsub = "select * from lty_subcategory where subcatid='$subcatid' ";
$ressub = mysql_query($sqlsub) or die (mysql_error());
$show = mysql_fetch_object($ressub);
$subname1 .= $show->subcatname.",";
$tyu = "select * from lty_category where catid='$catid'";
$uti = mysql_query($tyu) or die (mysql_error());
$view = mysql_fetch_object($uti);
$catname = $view->catname;
$bottomsub = $subname;
$bottomcat[$i] = $catname;
$i++;
}
}
$subsname = substr($subname1,0,strlen($subname1)-1);
$catsname = substr($subname2,0,strlen($subname2)-1);
$ckval= "UserName :".$logname."<br> My Top 5 : ".$catsname."<br> My Bottom 10 : ".$subsname;
//...............................................................
$new = "";
}else
{
$ckval = "Cannot Read the Cookie Value. Please check your Browser Settings";
$new = "";
}
include("replaceval.js");
?>
Contents of Replaceval.js:
// JavaScript Document
//alert("Yes");
var kword = '<? echo $ckval; ?>' ;
var ckval = '<? echo $new; ?>' ; //Getting the keyword and replace text from the hitlog page
//alert(kword);
//alert(ckval);
function replaceAll(oldStr,findStr,repStr) {
srchNdx = 0; // srchNdx will keep track of where in the whole line
// of oldStr are we searching.
var newStr = ""; // newStr will hold the altered version of oldStr.
while (oldStr.indexOf(findStr,srchNdx) != -1)
// As long as there are strings to replace, this loop
// will run.
{
newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));
// Put it all the unaltered text from one findStr to
// the next findStr into newStr.
newStr += repStr;
// Instead of putting the old string, put in the
// new string instead.
srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
// Now jump to the next chunk of text till the next findStr.
}
newStr += oldStr.substring(srchNdx,oldStr.length);
return newStr;
}
if(document.getElementById("cookieval")) //This code is for IE problem pages.There placed a div tag with id 'xray_Track'
{
texttorepl=document.getElementById("cookieval").innerHTML; //Get the content within the div tag
document.getElementById("cookieval").innerHTML=replaceAll(texttorepl,'value',kword); //Replace string with keyword and place back.
}