Welcome! Log In Create A New Profile

Advanced

Task 5c Setting Session $vars Mac please adiveconfused smiley

Posted by koot 
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
Task 5c Setting Session $vars Mac please adiveconfused smiley
September 14, 2010 03:23PM
I had some trouble with this topic. The script in the apply your knowledge didnt seem to work on my WAMP im currently running PHP 5.3 i did alter the code to look like this:

on session.php

<?php
session_start();

$bgcolor = $_SESSION['bgcolor'];

if (isset($_GET['color']))
$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";
}

$_SESSION['bgcolor'] = $bgcolor;

?>
<html>
<head>
<title>Sessions</title>
</head>

<body bgcolor="<?php echo $bgcolor; ?>" />
<br />
<form method="get" action="session.php?<?php echo "PHPSESSID".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 Color" />
</form><br />
<br />
<a href="session.php?<?php echo "PHPSESSID".session_id(); ?>" > Refresh Page, Dont change background color</a><br />
<br />
<a href="session2.php?<?php echo "PHPSESSID".session_id(); ?>"> To Other Page </a>
</body>
</html>

This worked for me because session_register is deprecated.

and session2.php

<?php
session_start();

$bgcolor = $_SESSION['bgcolor'];

if (empty($bgcolor)) {
$bgcolor = "white";
}
?>
<html>
<head>
<title>Session Example</title>
</head>

<body bgcolor="<?php echo $bgcolor; ?>">

What color is my background?<br />
<br />
<a href="session.php?<?php echo "PHPSESSID".session_id(); ?>" /> Back to form </a>
</body>
</html>

Is this acceptable according to the course Mac?
avatar
Mac
Re: Task 5c Setting Session $vars Mac please adiveconfused smiley
September 15, 2010 07:32AM
There is no correct way to write your code - although there are guidelines and best practises. If it works, then it works!

Remember the code in the manual is PHP4 therefore you had to upgrade your code... which is what you achieved and therefore a lesson learned smiling smiley
Re: Task 5c Setting Session $vars Mac please adiveconfused smiley
September 15, 2010 08:59AM
Thanks Mac much appreciated, I see now why they call php a open source language it can be easily adapted to suite your needs if you stick to the guidelines and best practices as you mentioned above. smileys with beer
Sorry, only registered users may post in this forum.

Click here to login