Forum Moderators: coopster

Message Too Old, No Replies

php style swithcer problem

         

kumarsena

1:48 pm on Oct 27, 2003 (gmt 0)

10+ Year Member



hey,

been trying to work on a style swithcer for my site using cookies. it works but when i use php query strings to pass a value to a script, to make the style wicth by clicking on a link, i canot get it to work. the code is below can someone please help me. its not my code as i found it on

[alistapart.com...]

swticher:

<?php
setcookie ('sitestyle', $set, time()+31536000, »
'/', 'yourdomain.com', '0');
header("Location: $HTTP_REFERER");
?>

link:

<a href="./switcher.php?set=red">
click here to change to RED style!</a>

style reference:

<link rel="stylesheet" type="text/css"
media="screen" title="User
Defined Style" href="<?php echo
(!$sitestyle)?'defaultstyle':$sitestyle?>.css" />

ive checked the code so many times without finding anything, so any help is appreciated..

thanks

kumar

ukgimp

2:02 pm on Oct 27, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



do you know if you have global variable off or on.

washrack.com/wasim/blog/index.php?textid=36

it will make a difference. If you are3 tring to collect the variakes one way a but your server is set up another way. Run a phpinfo() to see.

[uk.php.net...]

Then adjust your code accordingly.

kumarsena

2:22 pm on Oct 27, 2003 (gmt 0)

10+ Year Member



thanks,

but being new to php im not sure how to use the phpinofo() function. any sugesstions. what if global var is off, how can i work my self around that problem?

thnaks for any help
kumar

kumarsena

2:49 pm on Oct 27, 2003 (gmt 0)

10+ Year Member



me again,

found a way to get the results of the phpinfo() function, but cant say i understand cause there is so much info.

can anyone please go to the following address and tell em if glabal vars is on or off...?

i still need help to get around the problem if it is off.
thanks
kumar

[edited by: jatar_k at 5:16 pm (utc) on Oct. 27, 2003]
[edit reason] removed personal url [/edit]

kumarsena

3:21 pm on Oct 27, 2003 (gmt 0)

10+ Year Member



me again

global variable must be on since i have been using the following and it works,

<?php
$skin = $HTTP_COOKIE_VARS["skinname"];
?>

if im rite, the $http_cookie_vars is a global var.

which mekas it even harder to understand the initial code i posted...

jatar_k

5:30 pm on Oct 27, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



the setting you are looking for in phpinfo is register_globals. Yours is set to "ON".

Your reasoning for why is wrong actually.

$HTTP_COOKIE_VARS["skinname"]

can be used with register_globals on or off. If it is on then you can use just $skinname. If it is off you have to use the syntax above. That particular syntax is for php 4.1.0 and below, you should use $_COOKIE['skinname'] since you have 4.1.2

take a look through these
PHP Predefined Variables [ca.php.net]

redirecting based on $HTTP_REFERER is not a very reliable way to do things as it is not always present for you to use.

What exactly is the problem with the code? Where does it get to before it stops working? What behaviours are not occuring? Are you getting any error messages?

You'll have to give us a little better explanation of the situation so we can understand what exactly is happening.

kumarsena

8:26 am on Oct 28, 2003 (gmt 0)

10+ Year Member



hey, thanks i didnt know that one.

well, as for code, it deosnt give me any parsing errors.

the default style that i set is working, but it does not want to change. I think there is a problem in writing the cookie. i went to the cookie folder in 'documents and settings' and could not find the cookie there even tough i ran the script. so that seems to be the problem.

by the way how can i avoid using $HTTP_REFERER?

thanks for the edit jatar_k

kumar

kumarsena

8:28 am on Oct 28, 2003 (gmt 0)

10+ Year Member



just a tought;

could it be that the problem is with the query string passed trough that link? that the cookie is set but with no value...?

jatar_k

5:12 pm on Oct 28, 2003 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



a few things to take a look at

serialize [ca.php.net] and unserialize [ca.php.net] for storing more complex data in the cookie.

sessions [ca.php.net] instead of cookies.

actually storing the pagename in either the cookie or the session to avoid the use of referrer.

kumarsena

3:02 pm on Oct 30, 2003 (gmt 0)

10+ Year Member



thanks,

i was just going trought he original code agaon, and found out that the cookie is not being set. i use a different script as well for the same skining purpose, suiing radio buttons to pass a var to the script, code as follows:

form:

<form action="setcookie.php3" method="post">

Choose preferred skin:<br /><br />

<input type="radio" name="skin" value="wolf.css">Wolf<br />

<input type="radio" name="skin" value="accguitar.css">Guitar<br />

<input type="radio" name="skin" value="watch.css">Time<br /><br />

<input type="submit" name ="button" value="Enter Site">
</form>

script:

<?php

$skin = $_POST["skin"];

setcookie("skinname",$skin,time()+7200);

header('location:http://url/welcome.php3');

?>

now the above works fine, and the cookie is set.

anu explanation to why this wors while the initial code i posted does would be appreciated, as not understanding this is driving me nuts!

initial code:

<?php

$set = $_GET['set'];

setcookie('sitestyle',$set, time()+7200, '/', 'my_url');

header('Location: '.$_SERVER['HTTP_REFERER']);
?>

im checking out the recomendations u made, thanks...

kumar