/    Sign up×
Community /Pin to ProfileBookmark

Can I update a Cookie value wherever I am on my site?

Hi!

I need to know how can i access a cookie (create on index.php) so i can modify a value wherever i am on my site?

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@pyroJan 15.2004 — $_COOKIE['name'] (or $HTTP_COOKIE_VARS['name'] for PHP < 4.1.0).
Copy linkTweet thisAlerts:
@turbauthorJan 15.2004 — but if i need to change, for example:

$_COOKIE['lang'] == "FR"

and i want to change "FR" for "EN"

I've try $_COOKIE['lang'] = "EN" but it seem that the cookie did'nt update!
Copy linkTweet thisAlerts:
@pyroJan 15.2004 — It should have worked fine. Take a look at this quick and dirty example:

[code=php]<?PHP
$_COOKIE['foo'] = "bar"; # set $_COOKIE['foo'] to bar
echo $_COOKIE['foo']; # echo $_COOKIE['foo']
$_COOKIE['foo'] = "baz"; # set $_COOKIE['foo'] to baz
echo $_COOKIE['foo']; # $_COOKIE['foo']
?>[/code]
Copy linkTweet thisAlerts:
@turbauthorJan 15.2004 — I know that's wierd but if i test your small script it's working but check this:

I've got 2 pages (one page show the result and the other change the cookie var)

PAGE1 (that contain a link to PAGE2)

echo $_COOKIE['foo'];



PAGE2 (that redirect to PAGE1)

if($_COOKIE['foo'] == "FR"){

$_
COOKIE['foo'] = "EN";

}else{

$_COOKIE['foo'] = "FR";

}

And when i'm redirect to PAGE1, i always got FR!

What i'm doing wrong??

Thanx
Copy linkTweet thisAlerts:
@pyroJan 15.2004 — We'll need to see your source code, please.
Copy linkTweet thisAlerts:
@turbauthorJan 15.2004 — This page change the cookie value
[code=php]
<?
//SET Cookie Update
if($_COOKIE['text'] != "13"){
$_COOKIE['text'] = "11";
}else{
$_COOKIE['text'] = "13";
}

//REDIRECTION
header("Location: ".$from.".php");
?>
[/code]


This page display the cookie value
[code=php]
<?
session_start();
if($_SESSION["id"] != session_id()){header('location: http://www.turbulencemedia.com/vc');}

//SET Sub-Section Session Variables
if(isset($t)){$_SESSION["type"] = $t;}
if(isset($c)){$_SESSION["catg"] = $c;}

$f = $_COOKIE["text"];

//DECODE Special Character
$convert = get_html_translation_table(HTML_ENTITIES);
$convert = array_flip($convert);
?>
<html>
<head>
<title>acces.visionclient.ca ::: interface administrateur</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" type="text/css" href="../../includes/style.css">
<script language="JavaScript">
function checkFill(){
if(document.create.Type.value == ""){
alert("Vous devez sélectionner un TYPE D'ÉVALUATION!");
document.create.Type.focus();
return false;
}
if(document.create.Exist.value == "" && document.create.NewFR.value == "" && document.create.NewEN.value == ""){
alert("Vous devez sélectionner une CATÉGORIE EXISTANTE ou créer une NOUVELLE CATÉGORIE!");
document.create.NewFR.focus();
return false;
}
if(document.create.Kind.value == ""){
alert("Vous devez sélectionner UN STYLE DE RÉPONSE!");
document.create.Kind.focus();
return false;
}
if(document.create.PointFR.value == "" && document.create.PointEN.value == ""){
alert("Vous devez entrer une QUESTION!");
document.create.PointFR.focus();
return false;
}
}
</script>
</head>
<body topmargin="20" marginheight="20">
<table width="730" height="100%" align="center" border="0" cellspacing="0" cellpadding="0">
<form name="create" action="add.php?action=point" method="post" onSubmit="return checkFill()">
<tr>
<td width="15" height="6"><img src="../../images/f-headerL.gif"></td>
<td width="700" colspan="2" class="topShadow"></td>
<td width="15"><img src="../../images/f-headerR.gif"></td>
</tr>
<tr>
<td height="27"><img src="../../images/f-barL.gif"></td>
<td class="frameBar">
<span class="tCarbon<?=$f?>"><b>visionclient.ca</b> &#155; zone d'accès &#155; </span>
<span class="tColor<?=$f?>"><b>administrateur</b></span>
</td>
<td align="right" valign="bottom" class="frameBar">
<a href="../index.php"><img src="../../images/i-home.gif" title="Accueil" border="0"></a>
<a href="../../support.php"><img src="../../images/i-support.gif" title="Support" border="0"></a>
<a href="../../logout.php"><img src="../../images/i-logout.gif" title="Logout" border="0"></a>
</td>
<td><img src="../../images/f-barR.gif"></td>
</tr>
<tr>
<td height="6"><img src="../../images/f-barShadowL.gif"></td>
<td class="barShadow" colspan="2"></td>
<td><img src="../../images/f-barShadowR.gif"></td>
</tr>
<tr>
<td height="58"><img src="../../images/f-navL.gif"></td>
<td class="headerNav">
<a href="point.php"><img src="../../images/b-back.gif" border="0"></a>
<img src="../../images/e-navSpacer.gif">

<input type="image" src="../../images/b-save.gif" border="0">
</td>
<td align="right" class="headerNav">
<!-- -->
<a href="../../zoom.php?txt=<?=$f?>&back=form%2FpointAdd"><img src="../../images/b-text.gif" border="0"></a>
<!-- -->
<img src="../../images/e-navSpacer.gif">
<a href="help/pointAdd.php"><img src="../../images/b-help.gif" border="0"></a>
</td>
<td><img src="../../images/f-navR.gif"></td>
</tr>
<tr>
<td class="frameL"></td>
<td colspan="2" bgcolor="#FCFCFC">
<table width="100%" height="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="3" colspan="5"><img src="../../images/e-mainSpacer.gif"></td>
</tr>
<tr align="center" valign="bottom">
<td height="40" colspan="4"></td>
</tr>
<tr>
<td width="5" height="45"><img src="../../images/f-listL.gif"></td>
<td width="225" align="center" class="listBackL">
<span class="tCarbon11">.type d'évaluation.</span> <br>
<select name="Type" class="menu" onChange="document.location='pointAdd.php?t='+this.value">
<?
//CONNECT
$db = mysql_connect('127.0.0.1', 'turb0103', '8WHw62');
mysql_select_db('turbulencemedia_com_-_1',$db);

if($_SESSION["type"] != ""){

//SELECT All Type
$sql = "SELECT * FROM formType WHERE Link = '".$_SESSION["type"]."'";
$result = mysql_query($sql) or die('Erreur : '.mysql_error());
$query = mysql_fetch_array($result);
?>
<option value="<?=$query["Link"]?>"><?=$query["Name"]?></option>
<?
}
?>
<option value="">&nbsp;- - -</option>
<?
//SELECT All Type
$sql = "SELECT * FROM formType WHERE Link >= '13' AND Link <= '15' ORDER BY Name ASC";
$result = mysql_query($sql) or die('Erreur : '.mysql_error());

//POPULATE Menu
while ($row = mysql_fetch_array($result)){
?>
<option value="<?=$row["Link"]?>"><?=$row["Name"]?></option>
<?
}
?>
</select>
</td>
<td width="240" align="center" class="listBackM">
<span class="tCarbon11">.catégorie existante.</span> <br>
<select name="Exist" class="menu">
<?
if($_SESSION["catg"] != ""){

//SELECT Selected Cat
$sql = "SELECT * FROM formPoint WHERE Type = '".$_SESSION["type"]."' AND Cat = '".$_SESSION["catg"]."' AND CatNameFR != ''";
$result = mysql_query($sql) or die('Erreur : '.mysql_error());
$query = mysql_fetch_array($result);
?>
<option value="<?=$query["Cat"]?>"><?=$query["CatNameFR"]?></option>
<?
}
?>
<option value="">&nbsp;- - -</option>
<?
if($_SESSION["type"] != ""){

//SELECT All Cat
$sql = "SELECT * FROM formPoint WHERE Type = '".$_SESSION["type"]."' AND CatNameFR != '' ORDER BY CatNameFR ASC";
$result = mysql_query($sql) or die('Erreur : '.mysql_error());

//POPULATE Menu
while ($row = mysql_fetch_array($result)){
$name = strtr($row["CatNameFR"], $convert);

//Trim Long Cat
if(strlen($name) > 30){
$name = substr(trim($name),0,30);
$name = substr($name,0,strlen($name)-strpos(strrev($name), " "));
$name = $name."...";
}
?>
<option value="<?=$row["Cat"]?>"><?=$name?></option>
<?
}
}

//DISCONNECT
mysql_close();
?>
</select>
</td>
<td width="225" align="center" class="listBackR">
<span class="tCarbon11">.style de réponse.</span> <br>
<select name="Kind" class="field">
<option value="">&nbsp;- - -</option>
<option value="17">Choix de réponses</option>
<option value="18">Réponse textuelle</option>
</select>
</td>
<td width="5"><img src="../../images/f-listR.gif"></td>
</tr>
<tr>
<td height="45"><img src="../../images/f-listL.gif"></td>
<td class="listBackL">&nbsp;</td>
<td align="center" class="listBackM">
<span class="tCarbon11">.nouvelle catégorie FR <b>
<input type="Text" name="NewFRL" readonly value="0" class="count">/50 max</b>.</span><br>
<input type="Text" name="NewFR" maxlength="50" class="field" onkeyup="document.create.NewFRL.value=this.value.length">
</td>
<td align="center" class="listBackR">
<span class="tCarbon11">.nouvelle question FR <b><input type="Text" name="PointFRL" readonly value="0" class="count">/50 max</b>.</span><br>
<input type="Text" name="PointFR" maxlength="50" class="field" onkeyup="document.create.PointFRL.value=this.value.length">
</td>
<td><img src="../../images/f-listR.gif"></td>
</tr>
<tr>
<td height="45"><img src="../../images/f-listL.gif"></td>
<td class="listBackL">&nbsp;</td>
<td align="center" class="listBackM">
<span class="tCarbon11">.nouvelle catégorie EN <b>
<input type="Text" name="NewENL" readonly value="0" class="count">/50 max</b>.</span><br>
<input type="Text" name="NewEN" maxlength="50" class="field" onkeyup="document.create.NewENL.value=this.value.length">
</td>
<td align="center" class="listBackR">
<span class="tCarbon11">.nouvelle question EN <b><input type="Text" name="PointENL" readonly value="0" class="count">/50 max</b>.</span><br>
<input type="Text" name="PointEN" maxlength="50" class="field" onkeyup="document.create.PointENL.value=this.value.length">
</td>
<td><img src="../../images/f-listR.gif"></td>
</tr>
<tr>
<td height="5" colspan="5" class="listF"></td>
</tr>
<tr>
<td height="100%" colspan="5"></td>
</tr>
<tr>
<td height="40" colspan="5"></td>
</tr>
<tr>
<td height="60" valign="top" colspan="5"><img src="../../images/e-mainSpacer.gif"></td>
</tr>
</table>
</td>
<td class="frameR"></td>
</tr>
<tr>
<td height="6"><img src="../../images/f-footerL.gif"></td>
<td class="botShadow" colspan="2"></td>
<td><img src="../../images/f-footerR.gif"></td>
</tr>
<script language="JavaScript">
document.create.Type.focus();
</script>
</form>
</table>
</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@turbauthorJan 15.2004 — in this example, i use the var $_COOKIE['text'] to change the font size (11px or 13px)
Copy linkTweet thisAlerts:
@pyroJan 15.2004 — It looks like it will always equal 11.
Copy linkTweet thisAlerts:
@turbauthorJan 15.2004 — why????
Copy linkTweet thisAlerts:
@pyroJan 15.2004 — Because unless you have more code, you are checking if the cookie is not equal to 13 (it won't be the first time, as it won't even be set, and subsequent times, it will equal 11), set it to 11. Unless you have something that is supposed to toggel between the two values, it won't magically switch itself.
Copy linkTweet thisAlerts:
@turbauthorJan 15.2004 — Thank you for replying but

i'm not very good in English and have problem understanding what you're trying to tell me??

What I understand is that the cookie value can't be set to 13 in my script?
Copy linkTweet thisAlerts:
@turbauthorJan 16.2004 — Change this:

if($_COOKIE['text'] != "13"){

to:

if($_COOKIE['text'] == "13"){

but still not working????
Copy linkTweet thisAlerts:
@pyroJan 16.2004 — Do you have any sort of conditional that is supposed to be changing it? I see nothing in your code that would make me think it [i]should[/i] be doing that.
Copy linkTweet thisAlerts:
@turbauthorJan 16.2004 — seem that i'm in big s**t because i really don't understand why it's not working
Copy linkTweet thisAlerts:
@pyroJan 16.2004 — Take a look at your code, and let me know why you think it would change. I see nothing there that would make the cookie update in any way.
Copy linkTweet thisAlerts:
@turbauthorJan 16.2004 — I thing that this part can do it:

<?

//SET Cookie Update

if($_COOKIE['text'] == "13"){ //I've change != to ==

$_
COOKIE['text'] = "11";

}else{

$_COOKIE['text'] = "13";

}

//REDIRECTION

header("Location: ".$from.".php");

?>

Thank for your patience Pyro
Copy linkTweet thisAlerts:
@pyroJan 16.2004 — You want it to just choose between 13 and 11, every other time it runs? That's what that will do.
Copy linkTweet thisAlerts:
@turbauthorJan 16.2004 — Yes,

the user have 2 fontsize choice 11 and 13
×

Success!

Help @turb spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...