Welcome! Log In Create A New Profile

Advanced

Task 5C-1

Posted by Christopher 
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-1
March 10, 2008 03:23PM
Done
Re: Task 5C-1
March 22, 2008 05:49PM
Okay, I have been working on this for hours now! I have trawled the previous years forums, looked up a couple of tutorials on the net and read page 47 of the book and I AM STILL LOST. All the critical code in the masterskill book is incorrect -have to replace SID with session_id(); and we shouldn't use session_register() and so even the explanatory notes are confusing. Mac please could you give us some new explanatory notes on this and explain what the correct way of doing this is..pleeeeeeeeeeeeease...

My code changes the colour of the background but nothing I do actually keeps that colour once I have refreshed the page or clicked on the link to session2.php link. It seems quite a few people have had this problem in the past as well..

My code for session.php below:

<?php
session_start();
$bgcolor = $_SESSION["bgcolor"];
@extract($_POST);


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=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' name='submit'>
</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>
avatar
Mac
Re: Task 5C-1
March 23, 2008 08:23AM
Code is not incorrect.... it's for PHP4, which the course focuses on. You decide to do PHP5 code, you open yourself up for a lot of problems which one should really leave until you at least understand the basics of 4.

Looks more like your problem is related to the session not being initiated, or a cookie not being stored. That is not a PHP problem as such - but look at your php.ini settings under [sessions] - are the paths correct and do they exsit?

Try session.cookie_lifetime = 100000


Does the directory tmp exist? Is cookies enabled in your browser. Remember a session exist only for the time period you are on the site (or pages), so if you come back the session needs to be recalled via a cookie, otherwise the colour will not be remembered.
Re: Task 5C-1
March 24, 2008 08:33AM
Hi Mac

Thanks for the response. I am just so clueless at the moment that I can't even tell the difference between php4 and 5, I guess its confusing when the text book is 5 and the masterskill book is 4.

I checked the paths in my php.ini folder. THe session is being written to the tmp folder and I can see the sessions files being stored there so it seems as if there is something wrong with cookies? Cookies are enabled on my browser - i have included the [session] section of my php.ini folder just in case you can see something missing that I haven't? Thank-you..



[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
;
; As of PHP 4.0.1, you can define the path as:
;
; session.save_path = "N;/path"
;
; where N is an integer. Instead of storing all the session files in
; /path, what this will do is use subdirectories N-levels deep, and
; store the session data in those directories. This is useful if you
; or your OS have problems with lots of files in one directory, and is
; a more efficient layout for servers that handle lots of sessions.
;
; NOTE 1: PHP will not create this directory structure automatically.
; You can use the script in the ext/session dir for that purpose.
; NOTE 2: See the section on garbage collection below if you choose to
; use subdirectories for session storage
;
; The file storage module creates files using mode 600 by default.
; You can change that by using
;
; session.save_path = "N;MODE;/path"
;
; where MODE is the octal representation of the mode. Note that this
; does not overwrite the process's umask.
session.save_path = "C:\Program Files\xampp\tmp"

; Whether to use cookies.
session.use_cookies = 1

; This option enables administrators to make their users invulnerable to
; attacks which involve passing session ids in URLs; defaults to 0.
; session.use_only_cookies = 1

; Name of the session (used as cookie name).
session.name = PHPSESSID

; Initialize session on request startup.
session.auto_start = 0

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
session.cookie_lifetime = 100000

; The path for which the cookie is valid.
session.cookie_path = /

; The domain for which the cookie is valid.
session.cookie_domain =

; Handler used to serialize data. php is the standard serializer of PHP.
session.serialize_handler = php

; Define the probability that the 'garbage collection' process is started
; on every session initialization.
; The probability is calculated by using gc_probability/gc_divisor,
; e.g. 1/100 means there is a 1% chance that the GC process starts
; on each request.

session.gc_probability = 1
session.gc_divisor = 100

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440

; NOTE: If you are using the subdirectory option for storing session files
; (see session.save_path above), then garbage collection does *not*
; happen automatically. You will need to do your own garbage
; collection through a shell script, cron entry, or some other method.
; For example, the following script would is the equivalent of
; setting session.gc_maxlifetime to 1440 (1440 seconds = 24 minutes):
; cd /path/to/sessions; find -cmin +24 | xargs rm

; PHP 4.2 and less have an undocumented feature/bug that allows you to
; to initialize a session variable in the global scope, albeit register_globals
; is disabled. PHP 4.3 and later will warn you, if this feature is used.
; You can disable the feature and the warning separately. At this time,
; the warning is only displayed, if bug_compat_42 is enabled.

session.bug_compat_42 = 1
session.bug_compat_warn = 1

; Check HTTP Referer to invalidate externally stored URLs containing ids.
; HTTP_REFERER has to contain this substring for the session to be
; considered as valid.
session.referer_check =

; How many bytes to read from the file.
session.entropy_length = 0

; Specified here to create the session id.
session.entropy_file =

;session.entropy_length = 16

;session.entropy_file = /dev/urandom

; Set to {nocache,private,public,} to determine HTTP caching aspects
; or leave this empty to avoid sending anti-caching headers.
session.cache_limiter = nocache

; Document expires after n minutes.
session.cache_expire = 180

; trans sid support is disabled by default.
; Use of trans sid may risk your users security.
; Use this option with caution.
; - User may send URL contains active session ID
; to other person via. email/irc/etc.
; - URL that contains active session ID may be stored
; in publically accessible computer.
; - User may access your site with the same session ID
; always using URL stored in browser's history or bookmarks.
session.use_trans_sid = 0

; Select a hash function
; 0: MD5 (128 bits)
; 1: SHA-1 (160 bits)
session.hash_function = 0

; Define how many bits are stored in each character when converting
; the binary hash data to something readable.
;
; 4 bits: 0-9, a-f
; 5 bits: 0-9, a-v
; 6 bits: 0-9, a-z, A-Z, "-", ","
session.hash_bits_per_character = 4

; The URL rewriter will look for URLs in a defined set of HTML tags.
; form/fieldset are special; if you include them here, the rewriter will
; add a hidden <input> field with the info which is otherwise appended
; to URLs. If you want XHTML conformity, remove the form entry.
; Note that all valid entries require a "=", even if no value follows.
url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=,fieldset="
avatar
Mac
Re: Task 5C-1
March 25, 2008 06:09PM
Mmmm. This can take long. Understand that your page turns colour, which is what the code wants. So that's OK. It just doesn't remember it when you come back, which points at a configuration issue of XAMPP and/or your browser. Let that be for now. It probably has to do with XAMPP on your computer (which is an Windows installation issue). These problems hardly ever occur on a Linux server, which you are most likely to use. It may sound like I'm brushing this under the carpet, but the time spent to configure XAMPP on your installation is not worth it.The main difference between 4 and 5, is that our course focuses on 4. When you use 5, you have to make use of stuff like $_POST. Its effect is really minor give o0ur course content - which is aimed at teaching you the basis, but with which you can built pretty powerful applications.

We prefer 4 for this course, since it is much more intuitive. However, knowing that sooner or later 5 will be the standard (the signs are there), we want you to spent your money wisely, thus the recommended book for version 5.
Re: Task 5C-1
March 27, 2008 06:55AM
Okay,so it's likely to be a configuration issue...sjooooee, that makes me feel better!

Thanks for the 4 and 5 explanation as well. smile
Re: Task 5C-1
March 27, 2008 08:35PM
Completed thumbs up smiley
JwV
Re: Task 5C-1
April 22, 2008 09:28AM
Hi all

This one is giving me nightmares. Have checked the php.ini file (in C:\xampp\apache\bin). All seems well. don't have the session.cookie_lifetime = 100000 setting though. Can see the session file being created in the tmp dir. Can see the session id being passed from page to page. Yet no luck. I can change the color in the session.php page,yet when I click the refresh page button the background goes back to white. I then change the background again and then click on "To other page. The page loads for the first time with the 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.

I go back to the session.php page, change color once again, go to the other page. This time no error message, only a white background.


Session.php
------------

<?php
session_start();
$bgcolor = $_SESSION["bgcolor"];
@extract($_GET);

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 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 the 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_register ("bgcolor"winking smiley;
if (empty($bgcolor))
{
$bgcolor = "white";
}
?>
<html>
<head>
<title> session example </title>
</head>
<body bgcolor='<?php echo $bgcolor; ?>'>
What color is the background?
<a href='session.php?<?php echo session_id(); ?>'>Back to form page.</a>
</body>
</html>


Edited my php source to that sugested higher up in this chat. Made sure I am running php4 (Current PHP version in THIS XAMPP is PHP 4.4.7), but no luck. HELP. What now. Will continue reading though pervious years answers.

NEED HELP!!!!confused smileyconfused smileyconfused smiley
avatar
Mac
Re: Task 5C-1
April 23, 2008 07:23AM
echo $bgcolor and $color on your receiving page to make sure that these variable exists within that page. If they don't, then have not been passed/extracted.

eg
$bgcolor = $_SESSION["bgcolor"];
echo "bgcolour: $bgcolor";
and


@extract($_GET);
echo "colour: $color";

For some reason I have not figured out myself, sometimes using isset does not work as expected, so try if($color) as opposed to if(isset($color))

Also not sure if the code you posted is as you use it, since you're working very untidy here with losts of spaces etc. You need to be careful how you code

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

Me: <form method ="GET" action="session.php?<?php echo session_id(); ?>">
JwV
Re: Task 5C-1
April 23, 2008 03:24PM
Hi Mac

After a lot of google reading, redoing my code, reading forum threads back to 2005 on this matter and a going through a php help manual I downloaded I got it working. My code is as as follows :

code section for session.php
-------------------------------

<?php
session_start();
$bgcolor = $_SESSION['bgcolor'];
$color = $_POST['color'];
//extract($_POST);

if (isset($color))
{
switch ($color) {
case "Yellow":




took out the @extract statement - don't know what it does.
changed the GET in the form to a POST

replaced SID with session_id()

see ==>> <a href='session2.php?<?php echo session_id(); ?>'>To other page</a>

then just before the php block ends (for switch section) I inserted : $_SESSION['bgcolor'] = $bgcolor;

code section example :

-----------------------------------
break;
} // end switch
}
if (empty ($bgcolor))
{
$bgcolor = "white";
}
//load value.
$_SESSION['bgcolor'] = $bgcolor;

?>
<html>

--------------------------------------


Then my session2.php start looks as follows :

----------------------------------

<?php
session_start();
$bgcolor = $_SESSION['bgcolor'];

-----------------------------------

also relpace SID with session_id()

-----------------------------------------------------
What color is the background?
<a href='session.php?<?php echo session_id(); ?>'>Back to form page.</a>
</body>
-----------------------------------------------------


Now all works as it must.


Me - work untidy, lots of spaces??? I have always felt it made it more readable, and structured by spaceing the souece code.

you wrote :
-----------------------------------------------
You: <form method = GET action='session.php?<?php echo session_id(); ?>'>

Me: <form method ="GET" action="session.php?<?php echo session_id(); ?>">
--------------------------------------------------------------------------

Does the two line behave differnt from each other? Does it matter if one uses a " instead of a '. Does spacing effect the way php executes???


cool smiley
avatar
Mac
Re: Task 5C-1
April 24, 2008 06:59AM
I didn't see you using SID in your code you gave???
@extract extracts everything automtaically, so you don't have to do $this=['this'] and $that=['that'], whihc is usefull if you have a form with plenty variables. So that could not have been the problem, except if you use _GET when you should use _POST.

As for single versus double quotes, read http://www.jonlee.ca/php-tidbit-single-quotes-vs-double-quotes/
http://wolfprojects.altervista.org/outputinphp.php#singlevsdouble

Spacing is treated as characters, so too much space makes execution slower.
JwV
Re: Task 5C-1
April 24, 2008 10:52AM
Hi Mac

First, thanks for the two references on single versus double quotes. Made things alot clearer.

Regarding the SID. The source I posted first time round was after I edited it according to louisesanders, as seen higher up in the chat. Still I could not get it to work. After many a search and read I finaly got it right, and thus posted my findings.

This @extract function. Does it take a variable from a from from a previous page and load the its data into a like named field on the current page. Could you sugest a good site to read up on it.


I am still from the old school where we were taught to space out the source code to make it readable and structured. Oh well time to learn new tricks.

Thanks alot for the help.
avatar
Mac
Re: Task 5C-1
April 25, 2008 06:47AM
If you have a form with say name, surname, address, telh, telw, using @extract on the next page you don't have to go $name=$_POST['name'], $surname=$_POST['surname'] etc - you can just use $name, $surname etc.
Anonymous User
Re: Task 5C-1
May 25, 2008 05:55PM
Completed
Sorry, only registered users may post in this forum.

Click here to login