...

View Full Version : cookie does not last for long...



muhaidib
06-07-2005, 04:33 AM
hey all,, first i want you to go to this site,, www.7des.com

and just switch pages fast,, and try to change the styles,,,

the chosen style well not last for the whole season,, and also once you chose a style it will not set untill the page is refreshed,,

the code i have is this...


<?
if (isset($_POST['style']))
{
$style_sheet = $_POST['style'];
} else {
$style_sheet = "default";
}

setcookie('css',$style_sheet,time()+999999999);

?>

(the dropdown box is set to do the action in index.php where the top code is placed at the very top)

thanx alot :)

SeeIT Solutions
06-07-2005, 05:46 AM
how does your code choose which stylesheet to use?

Fou-Lu
06-07-2005, 04:38 PM
The larger problem I'm having is getting it to 'stick' on a theme. Its fine once its changed, some links will hold, others will not.
I don't recommend that you define it just with cookies, allow it on there nonetheless as this way the user can have it stick for them. Otherwise, your looking at using sessions.
Now, without knowing what you have set within your tracking I cannot tell you the best way of doing this. From the given code that we have the only tracking I can see thats being used is to set a cookie, not to read information after the fact. As I mentioned, cookies are also unreliable for generating information.
What I would do is like so:


<?php
ini_set(session.use_trans_sid, '1'); // Think this is right, I always have them enabled so I don't need to set it (try it without this line first, with cookies off)
session_start();

// Strongly recommend the use of this to confirm that the user isn't trying to pass their own data:
$allowed_styles = array(
'red',
'blue',
'green'
);

if (!isset($_SESSION['style']))
{
// Style is currently empty, let us check the request methods:
if (!isset($_POST['style']) AND isset($_COOKIE['style']) && in_array($_COOKIE['style'], $allowed_styles))
{
// Cookie is set for this style:
$_SESSION['style'] = addslashes($_COOKIE['style']);
}
else if (isset($_POST['style']) && in_array($_POST['style'], $allowed_styles))
{
// Cookies not set (or possibly set due to first condition) however post is set:
$_SESSION['style'] = addslashes($_POST['style']);
}
else
{
// Neither cookies, sessions, post, or allowed data is set, let us default it:
$_SESSION['style'] = 'default';
}
}

setcookie('css', $_SESSION['style'], time()+999999);

// Here, you will need to have whatever code to identify the page's css type

Just change whatever you are using to identify the css type from $_COOKIE to $_SESSION and it should work no problem for you. Of course, I haven't tested this though, so give it a shot somewhere where it won't cause you headaches first :P

muhaidib
06-07-2005, 09:50 PM
seeitseloutions,, i don't know what you mean sorry

and Fou-Lu

it does not seem to work,, same thing happens the style keeps changing on it's own the whole time,,, here is the code for index.php


<?
if (isset($_POST['style']))
{
$style_sheet = $_POST['style'];
} else {
$style_sheet = "default";
}

setcookie('css',$style_sheet,time()+999999999);

?>

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<?php
$style_sheet = !empty ($_COOKIE['css']) ? $_COOKIE['css'] : 'default';
echo '<link rel="stylesheet" type="text/css" href="styles/'.$style_sheet.'.css">';
?>
<title>7Designs Home</title>
</head>

<body>
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="800" id="table1" class="out" height="520">
<tr>
<td height="100" class="banner" colspan="2">&nbsp;</td>
</tr>
<tr>
<td width="800" colspan="2">
<?php include("includes/head.php") ?></td>
</tr>
<tr>
<td width="150" valign="top"><?php include("includes/links.php")?></td>
<td width="650" align="center" class="content_container"><?php echo $lang['welcome'] ?> <table border="0" cellpadding="0" cellspacing="0" width="600" id="table2" height="350">
<tr>
<td>
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="353" height="300" id="table3" class="content">
<tr>
<td class="content_title" height="21">What's New at 7Designs</td>
</tr>
<tr>
<td valign="top">
<ul>
<li>BETA test of the new version</li>
<li>Questions section is open for any
question you have</li>
<li>Version 5 was Released after a few weeks
of work </li>
</ul>
</td>
</tr>
</table>
</div>
</td>
<td>
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" width="200" height="300" id="table4" class="content">
<tr>
<td class="content_title" height="21">Latest
Topic/News at the <a href="/forum" class="cnt_t">Forums</a></td>
</tr>
<tr>
<td valign="top">Forum icon Battle in the battle
room<p>Shoutbox added</p>
<p>New background displays</p>
<p>New signatures in showcase</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

<p align="center"><span class="tail"><?php include("includes/tail.php") ?></span></p>

</body>

</html>

SeeIT Solutions
06-08-2005, 01:08 AM
Try this. It will just replace your code up until the same line


<?
if (isset($_POST['style']))
{
$style_sheet = $_POST['style'];
} else {
$style_sheet = !empty ($_COOKIE['css']) ? $_COOKIE['css'] : 'default';
}

setcookie('css',$style_sheet,time()+999999999);

?>

<html>

<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<?php
echo '<link rel="stylesheet" type="text/css" href="styles/'.$style_sheet.'.css">';
?>

Fou-Lu
06-08-2005, 02:55 AM
The only problem I see is its still reliant on cookies. I also goofed with my code above there as well, my setcookie name should have been 'style' not 'css'. After that, access for it would be set via $_SESSION['style'] instead of $_COOKIE['css'].
The problem is, assuming this is the same code on all of your pages, you are attempting to grab style from a post variable everytime, otherwise its set to default. Which is why your pages keep changing back. $_POST data does not send from page to page via links.

SeeIT Solutions
06-08-2005, 03:35 AM
That's what my code fixes.

muhaidib
06-08-2005, 06:12 AM
very well done Fou-Lu & SeeIT Solutions

i can't thank you enough,,, so there is one thing fixed :thumbsup:

the CSS changes right away!

www.7des.com

now another problem is still in the way,, the style swiched back to blue after 2 pages...

it will not stay that for the whole season.. you can check your self :rolleyes:

SeeIT Solutions
06-08-2005, 07:10 AM
It seems as thought the projects page doesnt have the right code in it.

Make sure you put that code I gave you on every page.

muhaidib
06-08-2005, 07:53 PM
ohhhhhhh i see,,, i will try now,,,

muhaidib
06-08-2005, 08:08 PM
perfect perfect perfect :D :D :D

its working and it stays in each page!

even when i leave the site at (green) and open the browser again it still stays at green :D

so the cookie is perfect :)

thank you all of you so much :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum