Welcome! Log In Create A New Profile

Advanced

Topic 5C

Posted by mauritz 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Topic 5C
July 29, 2009 11:20PM
I completed the topic, but I have too be honest, my script doesnt work the way I wanted it too. It changes the colour, but does not allow me to "keep the colour" when I click on the new link. I added a isset function to verify that the session actually gets set, and it works when we change the immediate colour, but it does not work when I click to goto the new file or reload the file.

Can someone shed some extra light on how to impliment the SID constant? I see it was used in the tutorial, but I would like to get some more info on how sessions actually passes variables to other pages through the session ID.

Language: PHP
<?php if(!isset($bgcolor)) { session_start(); $bgcolor = $_GET[';color';]; $_SESSION[';bgcolor';] = $bgcolor; } else { session_start(); }   if (isset ($color)) { switch ($color) { case "Yellow": $bgcolor="#FFCC00"; break; case "Blue": $bgcolor="#4682B4"; break; case "Silver": $bgcolor="#C0C0C0"; break; case "Beige": $bgcolor="#FFE4C4"; break; default: $bgcolor = "white"; break; } // end switch } if (empty ($bgcolor)) { $bgcolor = "white"; } ?> <html> <head><title>Session Example</title></head>   <body bgcolor=';<?php echo $bgcolor; ?>';>   <form method=GET action=';session.php?<?php echo SID; ?>';> Select a background color <br> <input name=';color'; TYPE=';radio'; VALUE=';Yellow';> Yellow <br> <input name=';color'; TYPE=';radio'; VALUE=';Blue';> Blue <br> <input name=';color'; TYPE=';radio'; VALUE=';Silver';> Silver <br> <input name=';color'; TYPE=';radio'; VALUE=';Beige';> Beige <br> <br> <input type=';submit'; value=';Change background color';> </form> <a href=';session.php?<?php echo SID; ?>';>Refresh page, Don';t change backgroudn color</a> <br><br> <a href=';session2.php?<?php echo SID; ?>';>To other page</a>     </body> </html>
avatar
Mac
Re: Topic 5C
July 30, 2009 07:09AM
Where is $_GET['color']?
Re: Topic 5C
July 30, 2009 11:11PM
Language: PHP
<?php if(!isset($bgcolor)) { session_start(); $bgcolor = $_GET[';color';]; $_SESSION[';bgcolor';] = $bgcolor; } else { session_start(); }

I put it in a if statement because I was afraid that the reason for the session not working was because the normal script was re-setting itself with a blank field.

My idea was to check if $bgcolor was set when the page loads, if not, then start a session and grab the session from the URL, alternatively if it is set, then just start the session
avatar
Mac
Re: Topic 5C
July 31, 2009 02:10PM
Oh, there it is.... oops . No, session_start() must be at the very top of the page, before any HTML or text is sent.

See here http://www.tizag.com/phpT/phpsessions.php
Re: Topic 5C
August 16, 2009 03:00PM
Mauritz ,

Dont use SID ,replace that with session_id() , it works just fine.

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
Re: Topic 5C
August 16, 2009 03:10PM
Done and dusted , everything works great , Ill put my code so u can se what u did wrong .

session.php

<?php

session_start();
$color = $_GET['color'];

if (isset ($color))
{
switch ($color)
{
case "Yellow":
$_SESSION['bgcolor']="#FFCC00";
break;
case "Blue":
$_SESSION['bgcolor']="#4682B4";
break;
case "Silver":
$_SESSION['bgcolor']="#C0C0C0";
break;
case "Beige":
$_SESSION['bgcolor']="#FFE4C4";
break;
default:
$_SESSION['bgcolor'] = "white";
break;
} // end switch
}
if (empty ($_SESSION['bgcolor']))
{
$_SESSION['bgcolor'] = "white";
}
?>
<html>
<head><title>Session Example</title></head>

<body bgcolor='<?php echo $_SESSION['bgcolor']; ?>'>

<form method=GET action='session.php?<?php echo session_id(); ?>'>
Select a background color
<br>
<input name='color' TYPE='radio' VALUE='Yellow'> Yellow
<br>
<input name='color' TYPE='radio' VALUE='Blue'> Blue
<br>
<input name='color' TYPE='radio' VALUE='Silver'> Silver
<br>
<input name='color' TYPE='radio' VALUE='Beige'> Beige
<br>
<br>
<input type='submit' value='Change background color'>
</form>
<a href='session.php?<?php echo session_id(); ?>'>Refresh page, Dont change background color</a>
<br><br>
<a href='session2.php?<?php echo session_id(); ?>'>To other page</a>
</body>
</html>


session2.php

<?php
$session["bgcolor"];
$bgcolor=$_POST['bgcolor'];
if (empty($bgcolor))
{
$bgcolor = "white";
}

?>
<html>
<head>
<title> Session Example </title>
</head>
<body bgcolor='<?php echo $bgcolor; ?>'>
What is my Background?
<a href='session.php?<?php echo session_id(); ?>'>To form page</a>
</body>
</html>

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
Anonymous User
Re: Topic 5C
August 18, 2009 06:46PM
Done with this one, after lots of trial, error and reading!
avatar Re: Topic 5C
August 24, 2009 10:03AM
Well thanks to Chilli I have completed 5C.

Please help - Below is my original code - my code looks the same as Chilli's unless I am not seeing something - and it doesnt work.

session.php

<?php

session_start();

$color=$_GET['color'];

if(isset($color)) {



switch ($color) {
case "Yellow":
$bgcolor="#FFCC00";
break;
case "Blue":
$bgcolor="#4682B4";
break;
case "Silver":
$bgcolor="#C0C0C0";
break;
case "Beige":
$bgcolor="#FFE4C4";
break;
default:
$bgcolor = "white";
break;
} // end switch


}

if(empty ($bgcolor)) {
$bgcolor = "white";
}
?>

<html>
<head><title>Session Example</title></head>
<body bgcolor='<?php echo $_SESSION['bgcolor']; ?>'>

<form method='GET'action='session.php?<?php echo session_id(); ?>'>


Select a background color
<br>
<input name='color' TYPE='radio' VALUE='Yellow'> Yellow
<br>
<input name='color' TYPE='radio' VALUE='Blue'> Blue
<br>
<input name='color' TYPE='radio' VALUE='Silver'> Silver
<br>
<input name='color' TYPE='radio' VALUE='Beige'> Beige
<br>
<br>

<input type='submit' value='Change background color'>
</form>

<a href='session.php?<?php echo session_id(); ?>'>Refresh page, Dont't change background color</a>
<br><br>
<a href='session2.php?<?php echo session_id(); ?>'>To other page</a>


</body>
</html>

session2.php

<?php

$session["bgcolor"];
$bgcolor=$_GET['bgcolor'];


if (empty ($bgcolor)) {


$bgcolor = "white";
}

?>

<html>
<head>
<title> Session Example </title>
</head>
<body bgcolor=<?php echo $bgcolor; ?>'>

What color is my background?

<a href='session.php?<?php echo session_id(); ?>'>Back to form page</a>

</body>
</html>

Thanks

Gayle
avatar
Mac
Re: Topic 5C
August 24, 2009 12:15PM
Mmm - not all Chilli's code is correct.

What is this? $session["bgcolor"];

The session does not have a name?
avatar
Mac
Re: Topic 5C
August 24, 2009 12:28PM
I'm writing rubbish - that is PHP4 - LOL!

method='GET'action='session.php - work neatly smiling smiley And session_start right at top,
<?php
session_start
there must be no whitespace between <?php and session_start


<body bgcolor=<?php echo $bgcolor; ?>'>
avatar Re: Topic 5C
August 24, 2009 02:29PM
Thanks for the reply Mac

I copied the method='GET'action='session.php directly from the book - should have thought that something was funny with it - just shows dont copy blindly confused smiley

Gayle
avatar
Mac
Re: Topic 5C
August 24, 2009 05:21PM
You mean from the CD... I'll have to have a look at that.... sorry.
Re: Topic 5C
August 24, 2009 06:36PM
thanks mac ill remove the white space

smoking smiley When all else fails , read instructions grinning smiley
smiling smiley Reading is a course on it's own ! smiling smiley
fcb
Re: Topic 5C
August 26, 2009 05:14PM
finally!

session.php

<?php
session_start();

$color = $_GET['color'];

if (isset($color)) {
switch ($color) {
case "Yellow":
$bgcolor="#FFCC00";
break;
case "Blue":
$bgcolor="#4682B4";
break;
case "Silver":
$bgcolor="#C0C0C0";
break;
case "Beige":
$bgcolor="#FFE4C4";
break;
default:
$bgcolor = "white";
break;
} // end switch
} else {
$bgcolor = $_SESSION['bgcolor'];
}

if (empty($bgcolor)) {
$bgcolor = 'white';
} else {
$_SESSION['bgcolor'] = $bgcolor;
}

?>
<html>
<head>
<title>Session Example</title>
</head>
<body bgcolor="<?echo $bgcolor; ?>">
...

session2.php
<?
session_start();
$bgcolor = $_SESSION['bgcolor'];
if (empty($bgcolor)) {
$bgcolor = 'white';
}
?>
Re: Topic 5C
August 27, 2009 07:31AM
what happened to session_register ("bgcolor"winking smiley;

I don't follow now, and I want to get it right.

can anyone help?
avatar
Mac
Re: Topic 5C
August 27, 2009 07:33AM
That is used in PHP4. XAMPP runs PHP5 (although you can set it to run as PHP4), therefore session_register has been replaced by session_start. Which will probably be replaced when PHP6 comes out smiling smiley
fcb
Re: Topic 5C
August 27, 2009 08:15AM
Read the header.php section on p.429

"By using session_start() at the beginning of your page, you are telling the application to allow you access to $_SESSION variables. Now you can set and retrieve session variables."
Re: Topic 5C
August 27, 2009 09:15AM
Thank you guys, Ill be reading for now...

(So if you created a website for a company using PHP4, you will suddenly get an agrevated client phoning you because your service provider now runs PHP5 and their site won't work?)
avatar
Mac
Re: Topic 5C
August 27, 2009 10:01AM
Most, if not all providers now run PHP5. As explained in previous forums, I stick with PHP4 in the manual, then provide you with PHP5 in the text book, and you figure out what changed from 4 to 5 and adapt your code. This experience will stand you good when PHP6 comes out, when things will change again - maybe not the similar changes. Remember that most of what works in 4 will work in 5. Using XAMPP, you are working with 5.

See here http://www.phpbuilder.com/columns/ian_gilfillan20051206.php3 and here http://jero.net/articles/php6. Don't worry if you do not understand everything now smiling smiley
Re: Topic 5C
August 27, 2009 01:43PM
I'm getting there. Linda.com helped me a bit, but my second page still stays white... AAAAAARGH!!
Re: Topic 5C
August 27, 2009 02:20PM
<?php

// H E L P !!

$SESSION["bgcolor"];
$bgcolor=$_POST['bgcolor'];

if (empty ($bgcolor)) {

$bgcolor = "white";
}

?>

<html>
<head>
<title> Session Example </title>
</head>
<body bgcolor='<?php echo $bgcolor; ?>'>
What is my Background?
<a href='session.php?<?php echo session_id(); ?>'>To form page</a>
</body>
</html>
Re: Topic 5C
August 28, 2009 06:56AM
Maybe its the flew but this thing is bowling me out a bit, and I can't think straight anymore. I'll try on Saturday again, and lets wait and see what I come up with. I refuse to be beaten top a pulp by this.

I must understand every piece of code here and be able to do this stuff without reference. I think sessions kicks cookies ass. I also see that its not always the code that lets us down but often the logic behind the solution.

Albert Einstein said.
If I have 60 min to save the world , I will spend 59 min on problem annallysis
avatar
Mac
Re: Topic 5C
August 28, 2009 08:54AM
What is this on its own? $SESSION["bgcolor"];
Re: Topic 5C
August 28, 2009 11:01AM
It looks like a function with an array.
avatar
Mac
Re: Topic 5C
August 28, 2009 01:58PM
You're misunderstanding me.... Like that it isn't anything. It is useless.
avatar Re: Topic 5C
August 30, 2009 09:42PM
Done as well.

I did struggle with this one, even with the forum feedback above. I understand your idea about PHP4 and PHP5 and our study material Mac, but I am of the opinion that when you are a noob, it would be better to be shown the right way to do things first time around, rather than the old way and then struggle to find out how the new version actually works.

Here is my code (it works):

session.php:
Language: PHP
<?php session_start();   $color = $_POST[';color';];   If (isset ($color)) { switch ($color) { case "Yellow": $_SESSION[';bgcolor';]="#FFCC00"; break; case "Blue": $_SESSION[';bgcolor';]="#4682B4"; break; case "Silver": $_SESSION[';bgcolor';]="#C0C0C0"; break; case "Beige": $_SESSION[';bgcolor';]="#FFE4C4"; break; default: $_SESSION[';bgcolor';] = "white"; break; } // end switch } // end if (isset ($color))   if (empty ($_SESSION[';bgcolor';])) { $_SESSION[';bgcolor';] = "white"; }   ?> <html> <head><title>Session Example</title></head>   <body bgcolor = ';<?php echo $_SESSION[';bgcolor';]; ?>';>   <form method=';POST'; action=';session.php?<?php echo session_id(); ?>';> Select a background color <br> <input name=';color'; TYPE=';radio'; VALUE=';Yellow';> Yellow <br> <input name=';color'; TYPE=';radio'; VALUE=';Blue';> Blue <br> <input name=';color'; TYPE=';radio'; VALUE=';Silver';> Silver <br> <input name=';color'; TYPE=';radio'; VALUE=';Beige';> Beige <br> <br> <input type=';submit'; value=';Change background color';> </form>   <a href=';session.php?<?php echo session_id(); ?>';>Refresh page, Don';t change background colour</a>   <br><br>   <a href=';session2.php?<?php echo session_id(); ?>';>To other page</a>   </body> </html>


session2.php
<?php
session_start();

if (empty($_SESSION['bgcolor'])) {
$_SESSION['bgcolor'] = "white";
}
?>

<html>
<head><title>Session Example</title></head>

<body bgcolor='<?php echo $_SESSION['bgcolor']; ?>'>

What color is my background?

<a href='session.php?<?php echo session_id(); ?>'>Back to form page</a>
</body>
</html>
avatar
Mac
Re: Topic 5C
August 31, 2009 07:34AM
> but I am of the opinion that when you are a noob, it would be better to be shown the right way to do things first time around, rather than the old way and then struggle to find out how the new version actually works.

You will thank me one day....... and in any event, this is the right way smiling smiley since the code for PHP is correct - you just need to add something that we also provide you with!
Re: Topic 5C
August 31, 2009 09:15PM
smileys with beerI Finally $_GET IT!!, Got this to work and I feel chuffed that I did.

($SESSION['bgcolor'])....... $_SESSION['bgcolor']




Session.php

<?php
session_start();

$color= $_GET['color'];

if (isset ($color)) {

switch ($color) {
case "Yellow":
$_SESSION['bgcolor']="#FFCC00";
break;
case "Silver":
$_SESSION['bgcolor']="#C0C0C0";
break;
case "Blue":
$_SESSION['bgcolor']="#000099";
break;
case "Beige":
$_SESSION['bgcolor']="#FFE4C4";
break;
default:
$_SESSION['bgcolor'] = "white";
break;
} // end switch
}
if (empty ($_SESSION['bgcolor'])) {
$_SESSION['bgcolor'] = "white";
}

?>

<html>
<head>
<title>Session Stuffs</title>
</head>

<body bgcolor='<?php echo $_SESSION['bgcolor']?>'>

<form method='GET' action='session.php?<?php echo session_id(); ?>'>
Select a background color
<br>
<input name='color' TYPE='radio' VALUE='Yellow'> Yellow
<br>
<input name='color' TYPE='radio' VALUE='Blue'> Blue
<br>
<input name='color' TYPE='radio' VALUE='Silver'> Silver
<br>
<input name='color' TYPE='radio' VALUE='Beige'> Beige
<br>
<br>

<input type="submit" value="Change background color" />

<a href="session.php?<?php echo session_id(); ?>">Refresh Page don't change</a>
<br />
<br />
<a href="session2.php?<?php echo session_id(); ?>">To Other Page</a>\

</form>

</body>
</html>




SESSION2.PHP

<?php
session_start();

if (empty ($_SESSION['bgcolor'])) {
$bgcolor = "white";
$message = "you looser its still not working";
echo $message;

}

?>
<html>
<head>
<title> Session Example </title>
</head>

<body bgcolor="<?php echo $_SESSION['bgcolor']; ?>">

<a href="session.php?<?php echo session_id();?>"> Lets go back and see if it still works</a>
</body>
</html>
Re: Topic 5C
September 03, 2009 11:52AM
Shhooo this was really hard, thanks to comments got it working. To be honest am still abit puzzled am going to be doing alot more reading to make sense of this.
avatar Re: Topic 5C
September 14, 2009 09:52PM
I get it sort of working, but when I do the "Refresh Page don't change background colour" link, I get a "Notice: Undefined index: color in C:\wamp\www\StudentPHP\session.php on line 4" error. I get the same error when I return from session2.php

Line 4 is
Language: PHP
$color = $_GET[';color';];

session.php
Language: PHP
<?php session_start();   $color = $_GET[';color';];   if (isset ($color)) { switch ($color) { case "Yellow": $_SESSION[';bgcolor';]="#FFCC00"; break; case "Blue": $_SESSION[';bgcolor';]="#4682B4"; break; case "Silver": $_SESSION[';bgcolor';]="#C0C0C0"; break; case "Beige": $_SESSION[';bgcolor';]="#FFE4C4"; break; default: $_SESSION[';bgcolor';] = "white"; break; } // end switch } if (empty ($_SESSION[';bgcolor';])) { $_SESSION[';bgcolor';] = "white"; } ?>   <html> <head><title>Session Example</title></head>   <body bgcolor=';<?php echo $_SESSION[';bgcolor';]; ?>';>   <form method=GET action=';session.php?<?php echo session_id(); ?>';> Select a background color <br> <input name=';color'; TYPE=';radio'; VALUE=';Yellow';> Yellow <br> <input name=';color'; TYPE=';radio'; VALUE=';Blue';> Blue <br> <input name=';color'; TYPE=';radio'; VALUE=';Silver';> Silver <br> <input name=';color'; TYPE=';radio'; VALUE=';Beige';> Beige <br> <br> <input type=';submit'; value=';Change background color';> </form> <a href=';session.php?<?php echo session_id(); ?>';>Refresh page, dont change background color</a> <br><br> <a href=';session2.php?<?php echo session_id(); ?>';>To the other page</a> </body> </html>

session2.php
Language: PHP
<?php session_start();   if (empty ($_SESSION[';bgcolor';])) { $bgcolor = "white"; }   ?> <html> <head> <title> Session Example </title> </head>   <body bgcolor="<?php echo $_SESSION[';bgcolor';]; ?>"> What color is my background? <br> <a href="session.php?<?php echo session_id();?>">Back to the form page.</a> </body> </html>

What am I doing wrong?

I'm completely stuck now. *grumble* Time for bed, methinks.

Chris
~~~~
"Cleverly disguised as a responsible adult."
Sorry, only registered users may post in this forum.

Click here to login