News:

Join the Facebook Fan Page.

Main Menu

Setting cookies.

Started by Waste, November 08, 2004, 01:15:46 PM

Previous topic - Next topic

Waste

Ok, so I have this code in a file named "theme.php":


<?php
if(isset($_GET['css'])){
switch (
$_GET['css']) {
case
'css1':
$stylesheet = '<link href="/theme/theme1/style.css" type="text/css" rel="stylesheet">';
$_SESSION['csschanger']=$stylesheet;
break;
case
'css2':
$stylesheet = '<link href="/theme/theme2/style.css" type="text/css" rel="stylesheet">';
$_SESSION['csschanger']=$stylesheet;
break;
case
'css3':
$stylesheet = '<link href="/theme/theme3/style.css" type="text/css" rel="stylesheet">';
$_SESSION['csschanger']=$stylesheet;
break;
case
'css4':
$stylesheet = '<link href="/theme/theme4/style.css" type="text/css" rel="stylesheet">';
$_SESSION['csschanger']=$stylesheet;
break;
default:
$stylesheet = '<link href="/theme/theme5/style.css" type="text/css" rel="stylesheet">';
$_SESSION['csschanger']=$stylesheet;
}
}
?>


I want it this code to set a cookie instead of using the session.

Could it be done like this:


<?php
if(isset($_GET['css'])){
switch (
$_GET['css']) {
case
'css1':
$stylesheet = '<link href="/theme/theme1/style.css" type="text/css" rel="stylesheet">';
setcookie (['csschanger']=$stylesheet)
break;
case
'css2':
$stylesheet = '<link href="/theme/theme2/style.css" type="text/css" rel="stylesheet">';
setcookie (['csschanger']=$stylesheet)
break;
case
'css3':
$stylesheet = '<link href="/theme/theme3/style.css" type="text/css" rel="stylesheet">';
setcookie (['csschanger']=$stylesheet)
break;
case
'css4':
$stylesheet = '<link href="/theme/theme4/style.css" type="text/css" rel="stylesheet">';
setcookie (['csschanger']=$stylesheet)
break;
default:
$stylesheet = '<link href="/theme/theme5/style.css" type="text/css" rel="stylesheet">';
setcookie (['csschanger']=$stylesheet)
}
}
?>

Saleh

no not like that!
setcookie takes this arguments:
setcookie ( string name [, string value [, int expire [, string path [, string domain [, int secure]]]]])

so one of your lines should look like this:

setcookie
('csschanger', $stylesheet, time()+3600*24*30, '/', 'DOMAIN_HERE');

this cookie will expire after one month

We don't need a reason to help people

Waste

Sorry for being a n00b about this.
I'm just starting out learning to code, but The string I have to call this script is:

Before the document type.

<?php
session_start
();
require(
"./theme.php");
?>


Inside the head of teh document.

<?php
echo ($_SESSION['csschanger'])? $_SESSION['csschanger']: '<link href="/theme/theme2/style.css" type="text/css" rel="stylesheet">' ;
?>



What would I need to change in this to make it work?

[Unknown]

Replace $_SESSION with $_COOKIE, and use the setcookie call mentioned.

-[Unknown]

Advertisement: