Page 2 of 6

Posted: Tue Jul 15, 2003 5:47 am
by Marco van Wijngaarden
okay... guess i'm one step further with SESSIONS then... but still, when i go to the third page i seem to have lost my variable:

---stap2.php---

this page receives the variables $bedrijf, $name, etc. from a form on the previous page (stap1.php) with the form action set to "stap2.php"

Code: Select all

$klantgegevens = array( 
"bedrijf"     =>    $bedrijf,
"naam"        =>    $naam, 
"adres"       =>    $adres, 
"postcode"    =>    $postcode, 
"woonplaats"  =>    $woonplaats, 
"telefoon"    =>    $telefoon, 
"email"	      =>    $email,
"afleveren"   =>    $afleveren
);
print ("$klantgegevens[naam]<BR>\n");
$completeorder = array(
"klantgegevens" => $klantgegevens
);
session_start(); 
$_SESSION['order'] = $completeorder; 
print ("{$completeorder[klantgegevens][naam]}<BR>\n");

---stap3.php---

this page receives the variables $regel1, $regel2, etc. from a form on the previous page (stap2.php) with the form action set to "stap3.php"

Code: Select all

session_start();
$data = array( 
"regel1"=>($regel1),
"regel2"=>($regel2), 
"regel3"=>($regel3), 
"regel4"=>($regel4), 
"regel5"=>($regel5), 
"regel6"=>($regel6), 
"regel7"=>($regel7),
"regel8"=>($regel8),
"regel9"=>($regel9)
); 
print ("$data[regel1]<BR>\n");
$completeorder["data"] = $data;
print ("{$completeorder[kantgegevens][naam]}<BR>\n");
print ("{$completeorder[data][regel1]}<BR>\n");
When i try to print the variables with the last lines in the script, it returns nothing! so what's wrong here?? :?:

Posted: Tue Jul 15, 2003 6:16 am
by Heavy
When you did session_register("data"), you got yourself a variable referenced to by typing $data in your script. That's an old way of doing it and should not be done. And I know you know that by now.

When you use the superglobal $_SESSION, you are no longer referring to sessionvar $_SESSION['data'] by typing $data.

$data is now just an ordinary variable, and $_SESSION['data'] is what gets carried on to the next page parsed.

I don't understand the language you use for varnames, so I give you this instead:

step1.php

Code: Select all

<?php
<form action="step2.php" method="post">
    <input type="text" name="phone" >
    <input type="text" name="email" >
    <input type="submit" value="send it">
</form>
?>
step2.php

Code: Select all

<?php
session_start();
//Put all posted into session:
foreach ($_POST as $key => $value){
    //put some validation here to make sure no posted bogus vars gets into session
    $_SESSION[$key] = $value;
}

//use what is in the session:
print_r($_SESSION);
?>
next_step.php_or_whatever_page_that_is_called_after_the_step2.php:

Code: Select all

<?php
session_start();
//Use the vars in session:
$_SESSION['phone'] = '+46 ' . $_SESSION['phone']; //added prefix of Sweden
$_SESSION['email'] = 'alias< ' . $_SESSION['email'] . '>';

//print them:
print_r($_SESSION); //EDIT: fixed a typo here...

?>
It is really simple. Start session, and then use the session as an array. always set whatever content should go into session using the $_SESSION array, always read vars from session using the $_SESSION array.

forget about session_register('varname'). That is old thinking.

Use it like you use the session('varname') collection in ASP - VBScript with Windows.

Posted: Tue Jul 15, 2003 6:26 am
by Heavy
Marco van Wijngaarden wrote:HUH? PHP? What's that? Let me find out if it is as helpfull and easy as they all say!?
Well. PHP is a bit like using linux. You can do bad things and good things pretty easy.

Its syntax might be advanced... It is pretty forgiving, doesn't complaint very loud about uninitialized variables and such. It has wonderful support for arrays. It has built in functions and extensions that can take your application "to the moon" without requiring any proprietary software connected to it.

In one aspect it is easy. It is all there, just use it.

In another aspect it is such a forgiving language that it might fool you sometimes. Believe me, you will fail some more times if you don't structure your code nicely. (That applies to all languages of course.) Remember all varnames ar case sensitive and that no function names or constants are.

EDIT: But it looks better if a function that is declared as getAllUserNames() is called by typing getAllUserNames() and not getallusernames() or GETALLUSERNAMES()

Posted: Tue Jul 15, 2003 7:53 am
by Marco van Wijngaarden
So to put it in other words:

On every next page i go, i open my <body> like this:

Code: Select all

&lt;?php
session_start ();
then if i need to print a specific variable from an array which has been posted by a previous page or an array that has been created earlier in the script, i should be able to do that by typing:

Code: Select all

print ("{$_SESSION[data][regel1]}");
also i prefer not to post all variables seperately, but put those into an array instead and then post the array... but i assume those array's created will also be posted as long as i open my <body> like this:

Code: Select all

session_start();
foreach ($_POST as $key => $value){ 
vars gets into session 
$_SESSION[$key] = $value; 
}
i am going nuts here cos none of the above seems to be working and i am sure that my provider has PHP4 so what could be the problem?

Posted: Tue Jul 15, 2003 8:36 am
by Heavy
Marco van Wijngaarden wrote:So to put it in other words:

On every next page i go, i open my <body> like this:

Code: Select all

<?php
session_start ();
Yes, that is the way to go if the script you run needs sessions.
Marco van Wijngaarden wrote:
then if i need to print a specific variable from an array which has been posted by a previous page or...
posted vars come in the $_POST array. Practically every aspect of that matter is discussed here.
Marco van Wijngaarden wrote:...or an array that has been created earlier in the script, i should be able to do that by typing:

Code: Select all

print ("{$_SESSION[data][regel1]}");
earlier in the script... hmmm.... THE session array $_SESSION holds session vars accessible from wherever you are after you've called session_start(); Other arrays than those declared withing $_SESSION, are completely gone after the script has finished.
Marco van Wijngaarden wrote: also i prefer not to post all variables seperately, but put those into an array instead and then post the array... but i assume those array's created will also be posted as long as i open my <body> like this:

Code: Select all

session_start();
foreach ($_POST as $key => $value){ 
vars gets into session 
$_SESSION[$key] = $value; 
}
open my <BODY>... that's talking HTML... You might not need to make an array anymore before posting, since all you post is in the array $_POST. Read about this in the topic i link to above. If you anyway prefer to post the data as an array, you can. Then, it will be accessible using the arrays name like this:
HTML code:

Code: Select all

&lt;form action="step2.php" method="post"&gt;
    &lt;input type="text" name="dataarr&#1111;phone]" &gt;
    &lt;input type="text" name="dataarr&#1111;email]" &gt;
    &lt;input type="submit" value="send it"&gt;
&lt;/form&gt;
php code in step2.php:

Code: Select all

<?php
session_start();
$_SESSION['dataarr'] = $_POST['dataarr']; 
//now, $_SESSION['dataarr'] contains two elements like this:
echo $_SESSION['dataarr']['phone'];
echo $_SESSION['dataarr']['email'];
?>
Then you have it as one array.
Marco van Wijngaarden wrote: i am going nuts here cos none of the above seems to be working and i am sure that my provider has PHP4 so what could be the problem?
run the phpinfo() function at the top of one script and see the system report. Read the version number and check that it is larger than PHP 4.2.x. Before then, the $_SESSION variable did not exist...

If your script is not extremely sensitive code, you might want to post all of it here and someone, maybe me, MIGHT (don't expect anyone to though) fix it for you?

Posted: Tue Jul 15, 2003 8:39 am
by Heavy
One more thing.
Don't go nuts... That doesn't help a bit.
It is not magic. it is just a bug. Read more, test more. Break complicated parts apart so you can see what is happening.

In the worst case, rewrite the troublesome part completely with your newfound experience. It might get better on next try.

Posted: Tue Jul 15, 2003 8:59 am
by Marco van Wijngaarden
To give you a good idea of the current status, my steps look like this:

---step1.php---

Code: Select all

<form name="klantgegevens" method="post" action="step2.php">

<P>Bedrijf <input type="text" name="bedrijf" size="50"></P>
<P>Naam <input type="text" name="naam" size="50"></P>
<P><input type="submit" name="Submit" value="To step 2">
</form>
---step2.php---

Code: Select all

<form name="data" method="post" action="step3.php">
<?php
session_start();

$klantgegevens = array( 
"bedrijf"     =>    $bedrijf,
"naam"        =>    $naam, 
);
		
print ("$klantgegevens[bedrijf]<BR>\n");
		
$_SESSION['klantgegevens'] = $klantgegevens;
	
print ("{$_SESSION[klantgegevens][bedrijf]}<BR>\n");
?>
		
<P>regel1<input type="text" name="regel1"></P>
<P>regel2<input type="text" name="regel2"></P>

<input type="submit" name="Submit" value="To step 3">
</FORM>
this page prints the value of array $klantgegevens[bedrijf]
also as a check if it went into SESSION:
this page also prints the same value from this variable:
$_SESSION[klantgegevens][bedrijf]

---step3.php---

this is where the trouble starts cos nothing seems to print from
$_SESSION[klantgegevens][bedrijf]

Code: Select all

<form name="check" method="post" action="step4.php">

<?php
session_start();
		
$data = array( 
"line1"=>($line1),
"line2"=>($line2), 
); 
		
$_SESSION = array("klantgegevens"=>$_SESSION[klantgegevens], "data"=>$data); 
		
print ("$data[line1]<BR>\n");
print ("{$_SESSION[data][line1]}<BR>\n");
print ("{$_SESSION[klantgegevens][bedrijf]}<BR>\n");		
?>
	
<p><input type="submit" name="Submit" value="To step 4"></p>
</form>
so to me it seems that on this page the variable

Code: Select all

$_SESSION[klantgegevens][bedrijf]
does not excist here anymore (it did print on the previous page), but what am i doing wrong here, is it caused by adding the second array "data" to $_SESSION???

Posted: Tue Jul 15, 2003 9:14 am
by Marco van Wijngaarden
mmmm.... PHP Version 4.0.6

could that be the problem? :cry: :cry: :cry:

Posted: Tue Jul 15, 2003 9:18 am
by Marco van Wijngaarden
run the phpinfo() function at the top of one script and see the system report. Read the version number and check that it is larger than PHP 4.2.x. Before then, the $_SESSION variable did not exist...
so what's the alternative? don't tell me it's cookies?! :wink:
maybe i better rest my case then... and start all over with a new subject on this forum regarding cookies... gosh!!! 8O

Posted: Tue Jul 15, 2003 9:34 am
by Marco van Wijngaarden
i dunno if this wil work, but in the early start of this topic, someone mentioned to add this to the next page:

<Input type="hidden" name="name" value="<?=$name?>">

in order to keep the variable... could i also do this an array instead of a single value?? then i guess i will be able to continue without use of cookies right?

Posted: Tue Jul 15, 2003 9:40 am
by Heavy
Here we go:

Firstly, it seems like you are programming with register globals on. That works but it is not recommended, since it is considered unsecure. Nevertheless is it harder to read which variables are posted or "getted" or created within the script.

Code: Select all

&lt;form name="klantgegevens" method="post" action="step2.php"&gt;

&lt;P&gt;Bedrijf &lt;input type="text" name="bedrijf" size="50"&gt;&lt;/P&gt;
&lt;P&gt;Naam &lt;input type="text" name="naam" size="50"&gt;&lt;/P&gt;
&lt;P&gt;&lt;input type="submit" name="Submit" value="To step 2"&gt;
&lt;/form&gt;
The above is OK.

---step2.php---

Code: Select all

<form name="data" method="post" action="step3.php">
<?php
session_start();

//You have to start session before any output!
//That is because when output has started, headers are send and cannot be changed once output has started.


$klantgegevens = array( 
"bedrijf"     =>    $bedrijf,
"naam"        =>    $naam, 
);
		
print ("$klantgegevens[bedrijf]<BR>\n");
		
$_SESSION['klantgegevens'] = $klantgegevens;
	
print ("{$_SESSION[klantgegevens][bedrijf]}<BR>\n");
?>
		
<P>regel1<input type="text" name="regel1"></P>
<P>regel2<input type="text" name="regel2"></P>

<input type="submit" name="Submit" value="To step 3">
</FORM>
So, do this instead:
step2.php:

Code: Select all

<?php
session_start();

$klantgegevens = array( 
"bedrijf"     =>    $_POST['bedrijf'],
"naam"        =>    $_POST['naam'], 
);
		
$_SESSION['klantgegevens'] = $klantgegevens;
		
echo "<pre>";
print_r ($klantgegevens);
print_r ($_SESSION['klantgegevens']);
echo "</pre>";
?>
<form name="data" method="post" action="step3.php">
<P>regel1<input type="text" name="regel1"></P>
<P>regel2<input type="text" name="regel2"></P>
<input type="submit" name="Submit" value="To step 3">
</FORM>
Now the session works... The print_r() function displays the content of any variable, even arrays and objects.

---step3.php---

Code: Select all

<?php
session_start();
//what do you mean lines? I guess you mean "regel".
$data = array( 
"regel1"=>($_POST['regel1']),
"regel2"=>($_POST['regel2']), 
); 
/*		
$_SESSION = array("klantgegevens"=>$_SESSION[klantgegevens], "data"=>$data); 
*/
//It is from this point of view OK to do that. But generally it is a no no, //since you set the whole session array with the result of your Array().
//Do this instead:

$_SESSION['data'] = $data;
// You dont need to set $_SESSION['klantgegevens'] again, it is already in the session.
		
print ("{$data['regel1']}<BR>\n");
print ("{$_SESSION['data']['regel1']}<BR>\n");
print ("{$_SESSION['klantgegevens']['bedrijf']}<BR>\n");		
?>
<form name="check" method="post" action="step4.php">
<p><input type="submit" name="Submit" value="To step 4"></p>
</form>
and in step 4 you just take care of what is in the session.

But here comes a more common (I think :? ) way to do this:
sessionless_step1.php:

Code: Select all

<?php
<form name="klantgegevens" method="post" action="sessionless_step2.php">
    <P>Bedrijf <input type="text" name="bedrijf" size="50"></P>
    <P>Naam <input type="text" name="naam" size="50"></P>
<P><input type="submit" name="Submit" value="To step 2">
</form>
?>
sessionless_step2.php:

Code: Select all

<?php
<form name="klantgegevens" method="post" action="sessionless_step3.php">
    <input type="hidden" name="bedrijf" value="<?php echo $_POST['bedrijf']?>>
    <input type="hidden" name="bedrijf" value="<?php echo $_POST['naam']?>>
   <P>regel1<input type="text" name="regel1"></P>
   <P>regel2<input type="text" name="regel2"></P>
<P><input type="submit" name="Submit" value="To step 3">
</form>
?>
sessionless_step3.php:

Code: Select all

<?php
<form name="klantgegevens" method="post" action="sessionless_step4.php">
    <input type="hidden" name="bedrijf" value="<?php echo $_POST['bedrijf']?>>
    <input type="hidden" name="bedrijf" value="<?php echo $_POST['naam']?>>
    <input type="hidden" name="bedrijf" value="<?php echo $_POST['naam']?>>
    <input type="hidden" name="regel1" value="<?php echo $_POST['regel1']?>>
    <input type="hidden" name="regel2" value="<?php echo $_POST['regel2']?>>
<P><input type="submit" name="Submit" value="To step 4">
</form>
?>
Unfortunately, this forum removes all my "?>" in the code above, so it gets broken.
EDIT: haha! that should be "[questionmark][greaterthan]"

The examples are a little ugly I think. It is because I don't like jumping in and out of PHP all the time. I did it now for you to understand :roll:

Posted: Tue Jul 15, 2003 9:45 am
by Heavy
Marco van Wijngaarden wrote:
run the phpinfo() function at the top of one script and see the system report. Read the version number and check that it is larger than PHP 4.2.x. Before then, the $_SESSION variable did not exist...
so what's the alternative? don't tell me it's cookies?! :wink:
maybe i better rest my case then... and start all over with a new subject on this forum regarding cookies... gosh!!! 8O
You didn't really read this did you?
viewtopic.php?t=511&start=0&postdays=0& ... highlight=

Session existed before. but not the new array $_SESSION. nor the $_POST I show you all the time.

Maybe you should get yourself a host that provides later version of PHP. 4.0.6 is awfully old. But sessions worked then too!

Posted: Tue Jul 15, 2003 9:49 am
by Heavy
As long as you stick with that old version. Do as in the beginning but be sure to start session BEFORE any output. Don't use $_SESSION or $_POST. Those are PHP 4.2+ Make sure the first <?php is on the first line of the script with no spaces or tabs to the left of it. Spaces, tabs and newlines are also output, thus destroying your mission.

Posted: Wed Jul 16, 2003 2:47 am
by Marco van Wijngaarden
Well, thanks for your input... indeed i never really understood that my providers PHP version could be the problem... i figured they'd be up to date actually... but anyway... i will have to do the older type of php then for now! I really like the idea of $_SESSION... meaning that variables will be available site wide, so as you say it is possible with my version... exactly how should i change my code in order to get this working on PHP 4.0.6..... also you mention "register_globals" that is my providers setting so i can't change that, right?

Posted: Wed Jul 16, 2003 3:27 am
by Heavy
Now we come to the point where you ask me to do your job :wink:
Marco van Wijngaarden wrote: ...so as you say it is possible with my version...
Yes, data storage in sessions are available in 4.0.6 as well.
Marco van Wijngaarden wrote:exactly how should i change my code in order to get this working on PHP 4.0.6.....
I won't show you exactly how. Learn yourself. Since you started out typing session_register() and stuff you probably know where to read about it.
Marco van Wijngaarden wrote:also you mention "register_globals" that is my providers setting so i can't change that, right?
Read about ini_get() and ini_set(). Use ini_get() to see wether register globals is on or off.

If it is off, you are still not very much in trouble, you just need to use other variables when doing posting and such. I found the following in a comment here:

Code: Select all

function fn_http_vars_access() &#123;

  global $GET_VARS, $POST_VARS, $COOKIE_VARS, $SESSION_VARS, $SERVER_VARS, $ENV_VARS;

  $parser_version = phpversion();

  if ($parser_version <= "4.1.0") &#123;
     $GET_VARS      = $GET_VARS;
    $POST_VARS     = $POST_VARS;
     $COOKIE_VARS   = $COOKIE_VARS;
    $SESSION_VARS  = $HTTP_SESSION_VARS;
     $SERVER_VARS   = $HTTP_SERVER_VARS;
     $ENV_VARS      = $HTTP_ENV_VARS;
  &#125;
  if ($parser_version >= "4.1.0") &#123;
     $GET_VARS      = $_GET;
     $POST_VARS     = $_POST;
     $COOKIE_VARS   = $_COOKIE;
     $SESSION_VARS  = $_SESSION;
     $SERVER_VARS   = $_SERVER;
     $ENV_VARS      = $_ENV;
  &#125;
&#125;
If sessions still doesn't work as expected, try this:

Code: Select all

function fn_http_vars_access() &#123;

  global $GET_VARS, $POST_VARS, $COOKIE_VARS, $SESSION_VARS, $SERVER_VARS, $ENV_VARS;

  $parser_version = phpversion();

  if ($parser_version <= "4.1.0") &#123;
     $GET_VARS      = $GET_VARS;
    $POST_VARS     = $POST_VARS;
     $COOKIE_VARS   = $COOKIE_VARS;
    $SESSION_VARS  = &$HTTP_SESSION_VARS;
     $SERVER_VARS   = $HTTP_SERVER_VARS;
     $ENV_VARS      = $HTTP_ENV_VARS;
  &#125;
  if ($parser_version >= "4.1.0") &#123;
     $GET_VARS      = $_GET;
     $POST_VARS     = $_POST;
     $COOKIE_VARS   = $_COOKIE;
     $SESSION_VARS  = &$_SESSION;
     $SERVER_VARS   = $_SERVER;
     $ENV_VARS      = $_ENV;
  &#125;
&#125;
...where we access $_SESSION or $HTTP_SESSION_VARS by reference. That means that any changes you make to the $SESSION_VARS is reflected onto the original array. But I have no experience of using $HTTP_SESSION_VARS, so I'm not sure if that array works like the $_SESSION array. Be sure to call fn_http_vars_access() before processing anything. BTW, you can do this regardless of the setting of register_globals. You shouldn't use register globals...

Read all the links I have provided in this post. They cover a lot of what you want to know. And when you are done reading, start considering the PHP Manual a valuable resouce. :wink: You learn best by doing it yourself.

If you are running Windows and want to get hold of a fresh PHP version with mysql and apache, you could try PHP Home . It installs all necessary things itself and you get to develop on a fresh version of PHP. Then get a host that likes to be up to date... :roll: