Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
Cookies...or is there a better way?
Old 08-10-2004, 11:48 AM Cookies...or is there a better way?
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Hey

I'm making a site using PHP/MySQL and i want the user to be able to change the skin (colours, images etc) used on the site to make things abit more interesting.

The best i way (..i think) would be to use cookies and when a alternative skin is chosen it would set a cookie to, for example, "blue" and when each page is loaded it would call this value from a cookie and incorporate it into the image file names, and CSS link names so the layout changes.

However, i dont really know where each bit of code should be, or what it should exactly be to be honest.... E.g. should the 'create cookie' code be a separate page that the form uses as its action, or should i be incorpaorated into each page.

If you understand this at all, any help would be appreciated. If you need something cleared up just post and ill try to clarify it a bit.

Cheers
Stoot
stoot98 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 08-10-2004, 12:21 PM
conkermaniac's Avatar
The Nutty Moderator

Posts: 1,012
Location: China
Trades: 0
Hi Stoot,

The code that sets the cookie should be placed on the submit page of the form. Then the cookie can be called from every other page on your website.

Conker
__________________

Please login or register to view this content. Registration is FREE
- Affordable feature-packed remotely hosted message boards!
conkermaniac is offline
Reply With Quote
View Public Profile
 
Old 08-10-2004, 04:32 PM
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Code:
$skin = $_POST['skin'];

setcookie("skin_id", $skin, "360000");

if (isset($skin_id)) {

IF ($skin_id == "blue") {

///display blue page

} {

//display red page

etc

}
} {

//display default

}

OK, this, i think, is fairly wrong but to get the general idea and to have something to work from. Would this be the sort of code i would have on the top of every page? Any changes or completely different versions kindly accepted!..

Also if the cookie was already there, and the user chose to change the skin again, would it overwrite the existing cookie with the new one?

Thanks for all the help, i appreciate it!
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 08-11-2004, 08:41 AM
david's Avatar
King Spam Talker

Posts: 1,314
Location: Glasgow, UK
Trades: 0
That looks like it would work. That code would only work for the submit page, though as you don't want to be setting the cookie on each page.

If the user chooses another colour, simply using setcookie again will cause the old cookie to be overwritten.

One minor point, for compatibility, instead of using:
PHP Code:
$skin_id 
to access your cookie value, you should use:
PHP Code:
$_COOKIE['skin_id'
__________________

Please login or register to view this content. Registration is FREE
- Everything a webmaster needs - for free

Please login or register to view this content. Registration is FREE
- Free web hosts reviewed and rated

Please login or register to view this content. Registration is FREE
- Impartial hosting directory - Add your host today for FREE
david is offline
Reply With Quote
View Public Profile
 
Old 08-11-2004, 05:57 PM
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
Thanks alot for the help so far guys! But i've not got it working....yet...

I've found a couple of tutorials about it and have some decent code in place which works in part but i have a few queries about it. First of all....here it is..

In the home.php page :

Code:
if (isset($_COOKIE["newskin"])) {
	
	$skin = $_COOKIE["newskin"];

}{

$skin = "1" ///set to default	

	
}

include("top$skin.htm");
After choosing the skin, it comes to this page which sets the cookies:

Code:
<?php 

// delete existing cookie
setcookie ("newskin", $skin_selection, time() - 3600, "/", "http://localhost/afi/new/");

// set new cookie
setcookie('skin',$skin_selection,time()+(86400*365),'/',"http://localhost/afi/new/");

?>
<html>
<body>
<p>Your site theme has been set to "<? echo $skin_selection ?>".</p>
<p align="right"><a class="button" href="home.php">OK</a></p>
</body>
</html>
The problems i'm having are:

1. The value being taken from the cookie seems to be ALWAYS "2", despite changing it.

2. The cookie doesn't seem to be deleted, i've found the cookie and it's as if new bits are just added to it...


"skin
3
localhost/
0
4258836352
29728438
2234645152
29655013
*
newskin
2
localhost/
0
2343803648
29728437
318612448
29655012
*
"

I'm getting REALLY frustrated with this, so any kind of help is needed. Thanks again for those who have replied and had a think about this. Cheers!

Stoot
stoot98 is offline
Reply With Quote
View Public Profile
 
Old 08-11-2004, 10:47 PM
JasonMichael's Avatar
Novice Talker

Posts: 13
Trades: 0
Try serializing your data before putting it into the cookie... in fact, I have a cookie update routine you might want to try that does this for you:

PHP Code:
function updateCookie($cookie,&$userinfo,$tm) {
   
$zone=-5;// eastern usa

        
$cookie serialize($userinfo );

      
$result setcookie('jmrtechnet'$cookietime() + $tm,"/","[your web adddress here]");
            
   
     

Also, check out PHP's webpage on the setcookie command. TONS of resources here.
JasonMichael is offline
Reply With Quote
View Public Profile
 
Old 08-12-2004, 05:08 PM
stoot98's Avatar
Ultra Talker

Posts: 427
Name: Stuart
Location: Glasgow, Scotland
Trades: 0
First of all, cookies hate me....

I couldnt get it to work despite all the help (thanks). I even tried the very basic cookie setting/retrieving code from tutorial sites but even that doesnt work....:/ I must be doing something consistenly wrong. Anyway, i'm gonna have to do it a different way which means it wont remember the users choice of skin and won't be as good....

Any more advice still welcome... bah...
stoot98 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Cookies...or is there a better way?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.25273 seconds with 12 queries