Welcome! Log In Create A New Profile

Advanced

Topic 5C-1

Posted by Labuschagne 
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-1
July 26, 2008 03:46PM
With a bit of effort I’ve completed this task. Because of using PHP5 and globals being turned off I had to find another way to complete this.

I was just wondering how everybody else did it, cause I think that I might have taken the long way round and was wondering if there’s a shorter way of doing it. I'm also worried that the variable is not being passed through session but in some other way.
I also had a problem with trying to set $bgcolor as my session variable.


For session.php:
<?php
session_start();
$_SESSION['color'] = $_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";
}

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

<body bgcolor='<?php echo $bgcolor;?>'>
<form method='get' action='session.php'>

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='Changes background color'>
</form>

<a href="session.php?color=<?php echo $_SESSION['color']; ?>">
Refresh page, Don't change background color
</a>
<br>
<br>
<a href="session2.php?color=<?php echo $_SESSION['color']; ?>">
To other page
</a><br><br>

</body>
</html>


Here’s session2.php (because of not using $bgcolor, I had to set the variable again):

<?php
session_start ();

$color = $_SESSION['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 $bgcolor; ?>">
<a href="session.php?color=<?php echo $_SESSION['color']; ?>">
Back to form page
</a>
</body>
</html>
Re: Topic 5C-1
August 08, 2008 02:54PM
completed 5A-1 successfully.

5B-1 seems successful, except that i don't get any 'delete cookie' link when i refresh the page, (either by cntl+F5 or refresh button). however, the radio buttons clear, in effect giving me a new options page. the colored page retains it's color when refreshed, so it appears as though the cookie is still active.(just as a matter of interest,in Vista, the cookies are stored in the following directory: C:\Users\user\AppData\Local\Microsoft\Windows\Temporary Internet Files[/b], and not in the more easily accessed C:\Windows\Cookies. the particular cookie also appears simply as StudentPHP/) - i hope that all makes sense!

now, as for 5C-1: no matter what i try, i have not been able to make this one work. i have even tried the above solution (from Labuschagne) which apparently works, but no matter what combinations i try, all i get is a bright green background and no response to the color selections. at one point, the second page background appeared in red regardless of what color i selected on the previous page! i have tried every combination of declaring session, declaring variables etc that i have been able to find, but no joy! aaaaargggggh!
avatar
Mac
Re: Topic 5C-1
August 11, 2008 07:19AM
Might not be related to your problem, but This before session_start is called, there must never be any white space (like created when you push the space bar).

Do some error checking first to see if the session works, i.e.
$color = $_SESSION['color'];
echo "colour: $color";
Re: Topic 5C-1
August 14, 2008 02:25PM
right, so after many hours & much juggling, i've managed to more or less get this one, tho still not completely (at least i'm not just getting a consistently green screen!).

this is what i have:


<?php
session_start();

error_reporting(E_ALL);

session_register ("bgcolor"winking smiley;
$color = $_SESSION['color'];
$_SESSION['color'] = $_GET['color'];
@extract($_GET);

echo "color: $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;
}

}

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, Dont change background color</a>
<br><br>
<a href='session2.php?<?php echo SID; ?>'>To other page</a>

</body>
</html>


...which gives me the correct color when selected; it also retains the correct color when i click 'Refresh page, Dont change background color'
though with this error message:
'Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0'

which i guess is expected considering the incomaptibil;ity issues whith PHP4/5.

when i click 'To other page', which looks like so:

<?php
session_start();

error_reporting(E_ALL);

session_register ("bgcolor"winking smiley;
$color = $_SESSION['color'];
//$_SESSION['color'] = $_GET['color']; (if i leave this line in, i get the same error message as previous page)
@extract($_GET);

echo "color: $color";

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

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

<body bgcolor='<?php echo $bgcolor; ?>'><br>

What color is my Background?

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

</body>
</html>


i get:
'color: Yellow (or whichever color i've chosen)
What color is my Background? Back to form page'

on a pure white page no matter what i do.


please put me out of my misery before i come adrift from my moorings altogether!!!!
avatar
Mac
Re: Topic 5C-1
August 15, 2008 07:10AM
Use $_POST to get the color rather than $_SESSION

But if you insist on using session, then this is occuring as a result of the security fix which prevents the direct transfer of global variables into the session array when register_globals is turned off.

$_SESSION['color'] = $_GET['color']; will cause this error

The quickest way around this restriction (without turning on register_globals, is to move the data over in two stages):

$color=$_REQUEST;
and then
$_SESSION=$color;
Re: Topic 5C-1
August 19, 2008 01:21PM
It's not that i insist on using SESSION, i would just like the task to work, whatever it takes.
Your suggested solution, i'm sorry to say though, is not working. more hours later & still no closer. It would appear there's no cookie; i can't find one where it's supposed to be.

ps. "Use $_POST to get the color rather than $_SESSION " did you mean 'use $_GET (the form method is GET)?

tristan
avatar
Mac
Re: Topic 5C-1
August 20, 2008 06:46AM
Yes. Is you XAMPP set to PHP5?
Re: Topic 5C-1
August 23, 2008 12:33AM
yes it is.

tristan
avatar
Mac
Re: Topic 5C-1
August 25, 2008 06:47AM
Then the issue lies with your security settings of your PC
Anonymous User
Re: Topic 5C-1
August 28, 2008 06:16PM
Indeed tricky but thanks to all the hints in the forums - done luckily!
Iris
Re: Topic 5C-1
September 19, 2008 10:27AM
done done done FINALLY thumbs up smiley

Justin Leaf-Wright

www.leafydesignz.co.za

hot smiley
Anonymous User
Re: Topic 5C-1
September 24, 2008 10:46AM
completed 5c-1 successfully.

Topic 5 was little bit tricky but managed to finish. I am finishing Topic 6 this week, and will be starting with project.

Mamog
Re: Topic 5C-1
September 24, 2008 07:50PM
Yeah I finally finnished this topic
,but learning by trial and error sucks ,the time could be better spent acquiring new knowledge
Re: Topic 5C-1
September 24, 2008 08:01PM
Yeah I finally finnished this topic

and the one before ,had problems passing the session variable ,until I used :


session_register('bgcolor'winking smiley;

in session2.php

and :

session_start();

$color = @$_GET['color'];

in session.php

I think I spent too much time on this,makes me unhappy
Anonymous User
Re: Topic 5C-1
September 24, 2008 08:53PM
done with 5C
smoking smiley Re: Topic 5C-1
September 30, 2008 10:02AM
Im done with 5C1smoking smiley

Ryan Harris
Re: Topic 5C-1
October 01, 2008 08:23PM
Done ages ago but forgot to post.

Completed Successfully with no problems what so ever! smiling smiley
Anonymous User
Re: Topic 5C-1
October 02, 2008 07:51AM
5C is completed and out the way! Now for some DB work......
Re: Topic 5C-1
October 09, 2008 06:44PM
i decided to cut myself some slack by turning on global variables in php.ini file; i was getting too frustrated at having to guess at workarounds while still trying to learn the basic syntax & concepts. Presumably, as this is an 'introduction to php', & seeing as how the study material is written for php4, i'm assuming that if we write our assignment to work in php4 environment, we won't be penalized on this score.

since turning on global variables, doing the remaining exercises from the study book has been a breeze as everything works the way it should. once i know the basics i can start to figure out all the workarounds for php5.

so, finally finished 5C!!!!!

tristan
avatar
Mac
Re: Topic 5C-1
October 10, 2008 07:05AM
You can write in 4 or 5. Point is, you should learn the difference between 4 and 5.
Re: Topic 5C-1
October 11, 2008 11:20AM
I struggled with this topic for ages! I found a very useful post on the Sem 1 forum by JwV.

Here is my code:

<?php
session_start(); //code will not function without starting the session
$bgcolor = $_SESSION['bgcolor']; //used this in stead of session_register("bgcolor"winking smiley;

echo "bgcolorrrrrrrrrrrrrrrrrrrrrr: $bgcolor";

$color = $_GET['color'];

if(isset($color)) {
switch ($color) {
case "Yellow":
$bgcolor="#FFCC00";
echo "THE BG COLOR IS: " . $bgcolor;
break;
case "Blue":
$bgcolor="#4682B4";
echo "THE BG COLOR IS: " . $bgcolor;
break;
case "Silver":
$bgcolor="#C0C0C0";
echo "THE BG COLOR IS: " . $bgcolor;
break;
case "Beige":
$bgcolor="#FFE4C4";
echo "THE BG COLOR IS: " . $bgcolor;
break;
default:
$bgcolor = "white";
echo "THE BG COLOR IS: " . $bgcolor;
break;
} // end switch

}

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

$_SESSION['bgcolor'] = $bgcolor; //my code will not work correctly without this piece of code. I'm not sure why you need to load the value again?

?>



<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, Don't change bckground color</a>
<br><br>
<a href='session2.php?<?php echo session_id(); ?>'>To other page</a>

</body>
</html>

I will appreciate it if someone can explain why you need to load the value again before the php block ends in session.php!
$_SESSION['bgcolor'] = $bgcolor; //my code will not work correctly without this piece of code. I'm not sure why you need to load the value again?
Sorry, only registered users may post in this forum.

Click here to login