Page 1 of 1

need help

Posted: Thu Dec 17, 2009 4:09 pm
by jaska94
Hi! I have just a little problem. I want to make a website where user can chose hes own style.
everything is working exept in the very first loading of the page there isnt any style, but why?
the error is Undefined index: css in C:\wamp\www\index.php on line 6. i can understand that theres no value but why?
also when i click the christmas style link i have to press reload to get it :|
stylechooser.php file looks like this:
<?php
$switch = (isset($_GET['mode'])) ? $_GET['mode'] : "index";

switch($switch) {
#option 1
case "christmas":
$css = "/christmas.css";
#setting session.
$_SESSION['css'] = $css;
break;

#option 2
case "fancy":
$css = "/fancy.css";
$_SESSION['css'] = $css;
break;

#reset to the default
default:
$css = "/default.css";
$_SESSION['css'] = $css;
break;
}
?>
And the index.php-s beginning looks like this :
<html>
<head>
<?php
session_start();
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$_SESSION['css']}\" media=\"screen\">";
?>
</head>

<title></title>
<body>
<?php include("style_chooser.php");?>
<?php include("visits.php");?>

<a href='?mode=christmas'>here</a>

Re: need help

Posted: Thu Dec 17, 2009 5:29 pm
by requinix
You have to call session_start before you can use $_SESSION.
jaska94 wrote:also when i click the christmas style link i have to press reload to get it :|
Could be caching. Or it could be a flaw in how your style switching code works.

Re: need help

Posted: Thu Dec 17, 2009 5:52 pm
by jaska94
added session start to the stylechooser file. Now it says :
A session had already been started - ignoring session_start() in C:\wamp\www\style_chooser.php on line 2

im pretty sure theres no session start nowhere else :D

Re: need help

Posted: Thu Dec 17, 2009 6:32 pm
by requinix
Oh, I didn't see the include(). In my defense it's painful to read text like that without nice coloring and formatting.

Code: Select all

<html>
<head>
<?php
session_start();
echo "<link rel=\"stylesheet\" type=\"text/css\" href=\"{$_SESSION['css']}\" media=\"screen\">";
?>
</head>
 
<title></title>
<body>
<?php include("style_chooser.php");?>
<?php include("visits.php");?>
 
<a href='?mode=christmas'>here</a>
You can't echo the $_SESSION["css"] until you determine what it is. And you don't do that until you include style_chooser.php.
Try moving some things around.

Re: need help

Posted: Sat Dec 19, 2009 11:12 am
by jaska94
thanx tasairis! i included the php file in wrong place , everything works now :D