...

View Full Version : does session_start() reset the session vars ?



jeddi
02-25-2008, 11:20 AM
Hello,

Am a bit new to using sessions and I have got a bit confused.

I am passing a session variable for the captha image.


My index.php starts off with:


<?php
/*
* index.php
*
*
* calls my_functions.php
* and first_page_fm.php
* and a_client_chk.php
* and b_email_act.php
* and confirm_disp.php
*
*
*/
session_start()

require_once("my_functions.php");



now later on it includes a form script:


require_once("first_page_fm.php"); // run BIG FORM
exit();


Inside the form the captcha script is run with the image
like this:


<span class="tab-cell-right" style="height: 40px; "><img src="captcha.php"> </a></span>


Inside captcha.php

the session var is set:


$_SESSION['key'] = md5($string);


Now the form script re-runs index.php
which processes the form data.

i.e.

<form name="main_fm1" action ='index.php' method = 'POST'>

So when the index re-runs (from the form) and the "session_start()" is encountered again, does the $_SESSION['key'] variable get lost ?

or does the "session_start()" pass it on ?

If passed on, this code should display it:



$keytxt =$_SESSION['key'];

ECHO "Keytext: $keytxt";


But it shows up as empty :o

Any ideas on what I am doing wrong ?

Thanks for any input.

CFMaBiSmAd
02-25-2008, 11:53 AM
Each page that sets or uses any session variable must contain a session_start() (how else do you suppose it works?). If captcha.php does not have a session_start() add one.

jeddi
02-25-2008, 01:03 PM
OK - thanks

I am going to put session_start() into the capctha.php

but I thought if it goes in there I will get those "headers already used" type error as this capctha.php is being called in the middle of my html form (after I've already sent lots of stuff to the browser) with the image statement like this:


<span class="tab-cell-right" style="height: 40px; "><img src="captcha.php"> </a></span>

If it doesn't error - why not ?

( just want to understand it better :o )

Thanks again

CFMaBiSmAd
02-25-2008, 01:11 PM
The URL listed in the <img src="..." alt=""> tag is fetched separately by the browser.

Here is how images on web pages work.

The browser requests a web page (such as your form.) The HTML code on that page is output to the browser. In your case you have an image and the HTML code is - <img src="captcha.php">. The browser sees the src="..." parameter and generates a http request to the web server to fetch the image. Any php code in captcha.php is not executed until the browser fetches the image.

tspiderus
10-12-2009, 07:35 AM
The URL listed in the <img src="..." alt=""> tag is fetched separately by the browser.

Here is how images on web pages work.

The browser requests a web page (such as your form.) The HTML code on that page is output to the browser. In your case you have an image and the HTML code is - <img src="captcha.php">. The browser sees the src="..." parameter and generates a http request to the web server to fetch the image. Any php code in captcha.php is not executed until the browser fetches the image.

I'm also trying to create a Captcha for a form. Inside "captcha.php" i try to register/store the code that is randomly generated: $_SESSION["captcha"] = 'xxx'; However, nothing is written in the session file.

The session_start() command is present at the begining of both files (captcha.php and .html).

What can I do?

Any help would be really apreciated since this issue has been driving me crazy for the past three days.

CFMaBiSmAd
10-12-2009, 11:55 AM
and .html
By default, php code is only parsed within .php files. Have you configured your web server so that it also parses php code in .html files? If not, you need to use .php files.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum