Jump to content

Archived

This topic is now archived and is closed to further replies.

phpasaurusREX

Session works in Firefox, but not in Internet Explorer

Recommended Posts

The goal is to design an W3C and Section 508 standards-compliant web site that gives the user the ability to customize the look and feel by selecting options from a drop-down list. I am a front-end designer, fluent in CSS and XHTML, but I have no experience with forms or back-end scripting. This is where I need help.

Here is the link to a demo of what I am trying to do: [a href=\"http://www.designx.biz/test\" target=\"_blank\"]http://www.designx.biz/test[/a]

If you open the above link in Firefox and start clicking through the links, then you should see how the site "remembers" your preferences as you go along. You can customize the style of the site to your liking, which should prove for a more enjoyable user experience. If you open the above link in Internet Explorer, however, then it does not change the styles when you click on the links. This is a problem. I chose to use standard hyperlinks for this demo, though I would like to make it work with XHTML drop-down form menus once I get the PHP working.

Here is a link to a ZIP file containing all the files in the above demo: [a href=\"http://www.designx.biz/test/test.zip\" target=\"_blank\"]http://www.designx.biz/test/test.zip[/a]

I am willing to keep an open mind, so feel free to suggest alternative means to meet my goal. The thought of storing the style preferences in cookies also crossed my mind, because it would be cool for the site to "remember" your style preferences if you re-visit the site after several reboots and a few days time. I am sure you all can see where I am going with this, so I hope you can help me!

Here is the code from switcher_background.php:

[code]
<?php
session_start();
if ( ! session_is_registered("background") ) {
    session_register("background");
    $_SESSION['background'] = 'background_black';
    $background = $_SESSION['background'];
    }
else {
    $background = $_SESSION['background'];
    }
if ( ! session_is_registered("color") ) {
    session_register("color");
    $_SESSION['color'] = 'color_yellow';
    $color = $_SESSION['color'];
    }
else {
    $color = $_SESSION['color'];
    }
if ( ! session_is_registered("size") ) {
    session_register("size");
    $_SESSION['size'] = 'size_small';
    $size = $_SESSION['size'];
    }
else {
    $size = $_SESSION['size'];
    }
if ( ! session_is_registered("font") ) {
    session_register("font");
    $_SESSION['font'] = 'font_verdana';
    $font = $_SESSION['font'];
    }
else {
    $font = $_SESSION['font'];
    }
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>StyleSheet Switcher</title>
<link rel="stylesheet" type="text/css" href="screen.css">
<link rel="alternative stylesheet" type="text/css" href="<?php echo $background ?>.css" title="User Defined Style">
<link rel="alternative stylesheet" type="text/css" href="<?php echo $alignment ?>.css" title="User Defined Style">
<link rel="alternative stylesheet" type="text/css" href="<?php echo $color ?>.css" title="User Defined Style">
<link rel="alternative stylesheet" type="text/css" href="<?php echo $size ?>.css" title="User Defined Style">
<link rel="alternative stylesheet" type="text/css" href="<?php echo $font ?>.css" title="User Defined Style">
</head>
<body>
Change Background Color<br>
<a href="switcher_background.php?background=background_black">Black</a><br>
<a href="switcher_background.php?background=background_red">Red</a><br>
<a href="switcher_background.php?background=background_green">Green</a><br>
<a href="switcher_background.php?background=background_blue">Blue</a><br>
<br>Change Alignment<br>
<a href="switcher_alignment.php?alignment=alignment_left">Left</a><br>
<a href="switcher_alignment.php?alignment=alignment_center">Center</a><br>
<a href="switcher_alignment.php?alignment=alignment_right">Right</a><br>
<br>Change Font Size<br>
<a href="switcher_size.php?size=size_small">Small</a><br>
<a href="switcher_size.php?size=size_medium">Medium</a><br>
<a href="switcher_size.php?size=size_large">Large</a><br>
<br>Change Font Color<br>
<a href="switcher_color.php?color=color_yellow">Yellow</a><br>
<a href="switcher_color.php?color=color_white">White</a><br>
<br>Change Font<br>
<a href="switcher_font.php?font=font_verdana">Verdana</a><br>
<a href="switcher_font.php?font=font_georgia">Georgia</a>
</body>
</html>
[/code]

Here is the code from switcher_background.php: (there are individual switchers for each style change)

[code]
<?php
session_start();
if ( ! session_is_registered("background") ) {
    session_register("background");
    }
$_SESSION['background'] = $_GET['background'];
header("Location: ".$_SERVER['HTTP_REFERER'] );
?>
[/code]

Share this post


Link to post
Share on other sites
<?php
session_start();
header("Cache-control: private");

Are you useing the header(); after the session_start(); ?

Share this post


Link to post
Share on other sites
[!--quoteo(post=377594:date=May 27 2006, 12:00 PM:name=Bob Leny)--][div class=\'quotetop\']QUOTE(Bob Leny @ May 27 2006, 12:00 PM) [snapback]377594[/snapback][/div][div class=\'quotemain\'][!--quotec--]
<?php
session_start();
header("Cache-control: private");

Are you useing the header(); after the session_start(); ?
[/quote]
I updated my original post to include the PHP code, let me know if this answers your question.

Share this post


Link to post
Share on other sites
Don't use session_register() and $_SESSION. Read the third caution box here:
[a href=\"http://us2.php.net/manual/en/function.session-register.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.session-register.php[/a]

Just use $_SESSION.

Take a look at our session troubleshooting guide:
[a href=\"http://www.phpfreaks.com/forums/index.php?showtopic=31047&view=findpost&p=157705\" target=\"_blank\"]http://www.phpfreaks.com/forums/index.php?...ndpost&p=157705[/a]

Share this post


Link to post
Share on other sites
Ok yeah, header("Cache-control: private"); is the fix to IE so place header("Cache-control: private"); imidetlly after the session_start(); on evrey page you have your sessions

Like this

<?php
session_start();
header("Cache-control: private");
if ( ! session_is_registered("background") ) {

Share this post


Link to post
Share on other sites
[!--quoteo(post=377603:date=May 27 2006, 12:15 PM:name=toplay)--][div class=\'quotetop\']QUOTE(toplay @ May 27 2006, 12:15 PM) [snapback]377603[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Don't use session_register() and $_SESSION. Read the third caution box here:
[a href=\"http://us2.php.net/manual/en/function.session-register.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.session-register.php[/a]

Just use $_SESSION.
[/quote]
If I take out the session_register(), then how will it know which session to start? For each style change (background, font, etc.) I need to use an alterative style sheet. So, if someone clicks on "red" as a background color, then the PHP needs to replace the file name in the HTML so the red style sheet is loaded.

[!--quoteo(post=377604:date=May 27 2006, 12:16 PM:name=Bob Leny)--][div class=\'quotetop\']QUOTE(Bob Leny @ May 27 2006, 12:16 PM) [snapback]377604[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Ok yeah, header("Cache-control: private"); is the fix to IE so place header("Cache-control: private"); imidetlly after the session_start(); on evrey page you have your sessions

Like this

<?php
session_start();
header("Cache-control: private");
if ( ! session_is_registered("background") ) {
[/quote]
I tried putting header("Cache-control: private"); on every page and it still does not work in IE.

Share this post


Link to post
Share on other sites
I think top play might be right.

Try chageing this...
[code]if ( ! session_is_registered("background") ) {
    session_register("background");
    $_SESSION['background'] = 'background_black';
    $background = $_SESSION['background'];
    }
else {
    $background = $_SESSION['background'];
    }[/code]

To this...
[code]
if (isset($_SESSION["background"]) ) {
    $_SESSION['background'] = 'background_black';
    $background = $_SESSION['background'];
    }
else {
    $background = $_SESSION['background'];
    }
[/code]

edit: I chaged it fro ! to the isset function which will check to see if the varieble exists.
lol sorry I think thats backwords!

Use this instead
[code]
if (isset($_SESSION["background"]))
{
    $background = $_SESSION['background'];
}
else
{
    $_SESSION['background'] = 'background_black';
    $background = $_SESSION['background'];
}
[/code]

Share this post


Link to post
Share on other sites
[!--quoteo(post=377614:date=May 27 2006, 12:50 PM:name=Bob Leny)--][div class=\'quotetop\']QUOTE(Bob Leny @ May 27 2006, 12:50 PM) [snapback]377614[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I think top play might be right.

Try chageing this...
[code]if ( ! session_is_registered("background") ) {
    session_register("background");
    $_SESSION['background'] = 'background_black';
    $background = $_SESSION['background'];
    }
else {
    $background = $_SESSION['background'];
    }[/code]

To this...
[code]
if (isset($_SESSION["background"]) ) {
    $_SESSION['background'] = 'background_black';
    $background = $_SESSION['background'];
    }
else {
    $background = $_SESSION['background'];
    }
[/code]

edit: I chaged it fro ! to the isset function which will check to see if the varieble exists
[/quote]

When I make these changes to the index.php file, it does not work in IE or Firefox.

Share this post


Link to post
Share on other sites
I think I might know a much easier way to do this! could I see a coulple of your style sheets?

Share this post


Link to post
Share on other sites
[!--quoteo(post=377624:date=May 27 2006, 01:08 PM:name=Bob Leny)--][div class=\'quotetop\']QUOTE(Bob Leny @ May 27 2006, 01:08 PM) [snapback]377624[/snapback][/div][div class=\'quotemain\'][!--quotec--]
I think I might know a much easier way to do this! could I see a coulple of your style sheets?
[/quote]

Here is a link to a ZIP file containing all the files: [a href=\"http://www.designx.biz/test/test.zip\" target=\"_blank\"]http://www.designx.biz/test/test.zip[/a]

Share this post


Link to post
Share on other sites
Very interesting, you have more than what I thought. If you don’t mind, id like to run a couple of tests to check a couple of things. This will take me an hour or 2 though.

Share this post


Link to post
Share on other sites
[!--quoteo(post=377630:date=May 27 2006, 01:23 PM:name=Bob Leny)--][div class=\'quotetop\']QUOTE(Bob Leny @ May 27 2006, 01:23 PM) [snapback]377630[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Very interesting, you have more than what I thought. If you don’t mind, id like to run a couple of tests to check a couple of things. This will take me an hour or 2 though.
[/quote]

Sure thing, I really appreciate it. I do not know what I am doing with PHP, so you might be able to find a much more simplified way of doing this.

Share this post


Link to post
Share on other sites
Pardon, but I tested your code (though very sloppy and way too long for it's intended pupose) and it works fine over here on IE, FF ánd Opera. I don't see the problem... Maybe you have FF set not to accept any cookies?

Share this post


Link to post
Share on other sites
[!--quoteo(post=377632:date=May 27 2006, 01:33 PM:name=448191)--][div class=\'quotetop\']QUOTE(448191 @ May 27 2006, 01:33 PM) [snapback]377632[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Pardon, but I tested your code (though very sloppy and way too long for it's intended pupose) and it works fine over here on IE, FF ánd Opera. I don't see the problem... Maybe you have FF set not to accept any cookies?
[/quote]

Ok, like I said this is my first time using PHP and I don't know what I am doing. I would appreciate suggestions on how to clean the code up and make it shorter, as opposed to just stating its sloppy and too long. By the way, [a href=\"http://www.designx.biz/test\" target=\"_blank\"]http://www.designx.biz/test[/a] works fine in FF. IE is the problematic browser. I have cookies enabled and default settings in both browsers. Could you double check [a href=\"http://www.designx.biz/test\" target=\"_blank\"]http://www.designx.biz/test[/a] in IE and make sure you can switch styles?

Share this post


Link to post
Share on other sites
Well I tried it on IE and it didnt work and I have cookies on. Im thinking sessions arnt cookies. maybe im wrong about that...

Share this post


Link to post
Share on other sites
When you debug, isolate.

To test if your [u]session script[/u] worked, I tesed only the php code! So you problem is NOT with the php code.

[code]<?php
session_start();
if (!session_is_registered('background') ) {
    session_register('background');
    $_SESSION['background'] = 'background_black';
    $background = $_SESSION['background'];
}
else {
    echo $_SESSION['background'];
}
if (!session_is_registered('color') ) {
    session_register('color');
    $_SESSION['color'] = 'color_yellow';
    $color = $_SESSION['color'];
}
else {
    echo $_SESSION['color'];
}
if (!session_is_registered('size') ) {
    session_register('size');
    $_SESSION['size'] = 'size_small';
    $size = $_SESSION['size'];
}
else {
    echo $_SESSION['size'];
}
if (!session_is_registered('font') ) {
    session_register('font');
    $_SESSION['font'] = 'font_verdana';
    $font = $_SESSION['font'];
}
else {
    echo $_SESSION['font'];
}
?>[/code]

On page refresh, this code echoes: "background_blackcolor_yellowsize_smallfont_verdana" which proves sessions are working fine. Your problem lies elsewhere.

[i]P.S.[/i] PHP session can use either 'get '(i.e. "index.php?sessionid=1f3870be274f6c49b3e31a0c6728957f") or a cookie to pass the session id across pages... Having your php.ini set to use cookies when a user has cookies disabled will cause sessions to fail, as php doesn't know which session to use.

Share this post


Link to post
Share on other sites
Ok this is what you want. Sorry it took so long! I wrote out the script and I had a friend (Hes alot better at php than I! Alot!) make some adjustments and fix my mistakes and it came out real nice!!!!

Ok page 1
[code]
<?php
session_start();
header("Cache-control: private");

if (isset($_SESSION["background"]))
{
    $background = $_SESSION['background'];
}
else
{
    $_SESSION['background'] = "#000000";
    $background = $_SESSION['background'];
}

if (isset($_SESSION["text"]))
{
    $text = $_SESSION['text'];
}
else
{
    $_SESSION['text'] = "#FFFFFF";
    $text = $_SESSION['text'];
}
?>
<html>
<head>
<style type="text/css">
<?php include(".06page3.php") ?>
</style>
</head>
<body>

<form action=".06page2.php" method="post">
Change Background Color:
<select name="background">
<option value="#000000">Black</option>
<option value="#FF0000">Red</option>
<option value="#00FF00">Green</option>
<option value="#0000FF">Blue</option>
</select>
<br />
Change Text Color:
<select name="text">
<option value="#FFFFFF">White</option>
<option value="#0000FF">Blue</option>
<option value="#FF0000">Red</option>
</select>
<br />
<input type="submit" value="Change!">
</form>


</body>
</html>
[/code]


Ok now page 2
[code]
<?php
session_start();
header("Cache-control: private");

$_SESSION['background'] = $_POST['background'];

$_SESSION['text'] = $_POST['text'];

echo "<META HTTP-EQUIV='Refresh' delay=.1 CONTENT= '.1; URL=.06page1.php'>"
?>
[/code]


Now you could of done this in 2 pages but so that you have something like an external style sheet we put the inline styles in an external page. So use this in place of your css file.
[code]
BODY
{
background-color: <?php echo $background; ?>;
color: <?php echo $text; ?>;
}
[/code]

Yeah working example at www.firemelt.net/.06page1.php Ill leave it up for a little while too

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.