Google Answers Logo
View Question
 
Q: PHP / Cookies ( No Answer,   2 Comments )
Question  
Subject: PHP / Cookies
Category: Computers > Internet
Asked by: trimidium-ga
List Price: $3.00
Posted: 17 Apr 2004 04:30 PDT
Expires: 17 May 2004 04:30 PDT
Question ID: 331648
Can Somebody rewrite my cookie script below to be simplier, so that
instead of me having to name all my objects image#.jpg i can simply
leave them with full length names and set the theme with the full
image name on the address line... example, currently:
index.php?setTheme=1  want:
index.php?setTheme=thenameof-image-whatever-it-may-be.gif

as an alternate i would be ok if i had to put a number before each
image, and still used the address line as setTheme=1 so long as i
could define completly different names with each image.

current script: 
<?php 
 
if (!empty($settheme)) { 
 $theme = $settheme; 
 setTheme(); 
} elseif (!empty($_COOKIE['theme'])) { 
 $theme = $_COOKIE['theme']; 
} elseif (!empty($killcookie)) { 
 killCookie(); 
} 
 
 
function setTheme() { 
 global $theme; 
 setcookie('theme',$theme,time()+2678500,'/','thesmartass.info'); 
} 
 
function getTheme() { 
 global $theme; 
  
 $default_theme = "random.php?pic=random1"; 
 
 $image_dir = "./images/artwork/"; // trailing slash is important. 
 $image_path = "images/artwork/"; // here too. 
 $image_prefix = "image"; 
 $image_suffix = ".jpg";
  
 if (!empty($theme)) { 
  $imagefile = $image_prefix.$theme.$image_suffix; 
  if (!file_exists($image_dir.$imagefile)) { 
   $imagefile = $default_theme; 
  } 
 } else { 
  $imagefile = $default_theme; 
 } 
  
 return $image_path.$imagefile; 
} 
 
function killCookie() { 
 setcookie('theme','',time()-2678500); 
} 

?>
Answer  
There is no answer at this time.

Comments  
Subject: Re: PHP / Cookies
From: okrogius-ga on 18 Apr 2004 17:34 PDT
 
Here you go. I also fixed up some security bugs, and made code more portable.

//allowed themes, for security purposes
$allowedThemes = array(
	'1.jpg',
	'whatever.gif'
);

//remove cookie function
function killThemeCokie() {
	setcookie('theme', '', time()-36000, '/' ,'thesmartass.info');
	header('Location: '.$_SERVER['PHP_SELF']);
	die();
}
//set cookie functiom
function setThemeCookie($name) {
	setcookie('theme', $name, time()+2678500, '/', 'thesmartass.info'); 
	header('Location: '.$_SERVER['PHP_SELF']);
	die();
} 

//do anything?
if ( !empty($_GET['killcookie']) )  {
	killCookie();
} elseif ( !empty($_GET['settheme']) && in_array($_GET['settheme',
$allowedThemes) ) {
	setThemeCookie($_GET['settheme']);
}

//what theme to use?
if ( !empty($_COOKIE['theme']) && in_array($_COOKIE['theme'],
$allowedThemes) && file_exists('./images/artwork/'.$_COOKIE['theme'])
) {
	$theme = 'images/artwork/'.$_COOKIE['theme']; //cokie
} else {
	$theme = 'random.php?pic=random1'; //default
}


Note that you need to udpate $allowedThemes with a list of all allowed
themes for purposes of security.
Subject: Re: PHP / Cookies
From: trimidium-ga on 18 Apr 2004 21:34 PDT
 
It doesn't work... 
1) what is the call function 
before it was getTheme();
you eliminated that function 

2) there are errors on the script that keep it from even masking the
code on a website... i set it up as an include on this page:
http://www.thesmartass.info/delete.php 
see for yourself, then go http://www.thesmartass.info/ to get the idea
of what i want it to ultimately do, i will need to make 2 different
cookie scripts to set 2 different images as backgrounds for the
website, they are currently both set to random

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy