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
avatar
Mac
Re: Topic 5C
September 15, 2009 08:35AM
You are sending just the session_id back to the page <a href='session.php?<?php echo session_id(); ?>'>Refresh page, dont change background color</a>

If you send this session_id to session2.php, you extract the color, but in session.php you extract the color a different way, thus the page does not have the bgcolor - it is undefined.
Re: Topic 5C
September 22, 2009 08:05AM
Done with 5C
avatar Re: Topic 5C
September 22, 2009 01:30PM
Topic complete.... interesting I can't help concluding that the sessions file /SID can be very useful in helping a webmaster with troubleshooting ... sort of like a log file....
Re: Topic 5C
October 01, 2009 06:20PM
5C Write php session variable

session1.php
<body>
<form id="form1" name="form1" method="post" action="session2.php">
<p>
<label for="name">Name:</label>
<input type="text" name="name" id="name" />
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>

session2.php
<?php
// initiate session
session_start();
// check that form has been submitted and that name is not empty
if ($_POST && !empty($_POST['name'])) {
// set session variable
$_SESSION['name'] = $_POST['name'];
}
?>
</head>
<body>
<?php
// check session variable is set
if (isset($_SESSION['name'])) {
// if set, greet by name
echo 'Hi, '.$_SESSION['name'].'. <a href="session3.php">Next</a>';
}
else {
// if not set, send back to login
echo 'Who are you? <a href="session1.php">Login</a>';
}
?>
</body>
</html>

session3.php

<?php
session_start();
ob_start();
?>
</head>
<body>
<?php
// check whether session variable is set
if (isset($_SESSION['name'])) {
// if set, greet by name
echo 'Hi, '.$_SESSION['name'].'. See, I remembered your name!<br />';
// unset session variable
unset($_SESSION['name']);
// invalidate the session cookie
if (isset($_COOKIE[session_name()])) {
setcookie(session_name(), '', time()-86400, '/'winking smiley;
}
ob_end_flush();
// end session
session_destroy();
echo '<a href="session2.php">Page 2</a>';
}
else {
// display if not recognized
echo 'Sorry, I don\'t know you.<br />';
echo '<a href="session1.php">Login</a>';
}
?>
</body>
</html>
Anonymous User
Re: Topic 5C
October 02, 2009 11:48AM
Done with this one. session_start() at the beginning of each page is a bit confusing. Maybe PHP Developers should rename it to something else. Something that will reflect its actual meaning.
Re: Topic 5C
October 07, 2009 03:57PM
Task 5C-1 was cool.Done
Re: Topic 5C
October 13, 2009 08:04PM
Done with 5C!
avatar Re: Topic 5C
October 15, 2009 10:03PM
Drat, I should actually comment when I finish things. I sorted it out when you posted a month ago, Mac. Work has been a bit hectic, so I've neglected to post about my progress. It hit me this evening, so here it is.

Done with 5C.

Chris
~~~~
"Cleverly disguised as a responsible adult."
Re: Topic 5C
November 06, 2009 05:59PM
I completed the topic 5c this my code
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;
}else {
$bgcolor = $_SESSION['bgcolor'];
}
// end switch
}
if(empty ($bgcolor)) {
$bgcolor = "white";
}else {
$bgcolor = $_SESSION['bgcolor'];
}
?>

<html>
<head><title>Session Example</title></head>
<body bgcolor='<?php echo $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_start( );
$bgcolor = $_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>
Sorry, only registered users may post in this forum.

Click here to login