Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

what's the right way to pass variable value to next pages ??

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

what's the right way to pass variable value to next pages ??

Postby fielding » Sun Nov 03, 2002 8:37 pm

Hi, Guys, I read several posts about the session_register function. But I find I can't success in the way.
my problem is: I am design a webbased registration system. I have three (more) pages in this site.
--- the first page is the login page,
--- the next page (upon login successed) is the function choice (say, to view personal information, to update personal information ..).
----the third page is the selected page based on the second page choice.

on the second page, buttons are linked to the designated pages. here are the codes on the second page:
Code: Select all
<?php
session_start();
$user_id = $_POST['userid']; //userid passed from the login page
session_register($user_id);
echo "in the post: user_id: $user_id";// here the value is passed
$i=$_SESSION['user_id'];
echo "in the session user_id: $i";//here the value is passed
?>

<html>
<head> <title>Student User Interface</title> </head>

<body>
 
  <table width="75%" border="1" cellspacing="20">
    <tr>
      <td>
<form action="student_pers_inf_view_page.php" method="post" name="form1" target="_parent">
<input type="submit" name="Submit" value="View Personal Information">
   </form>
</td>   //end of button one
.
.
.
the other buttons are the same as above, each button is an indepent form.

</body>
</html>


according to my imagination, after I push the view personal information button, the information should be display in the third page. but the problem is that the userid can't be passed from page two to the third one.

I tried to trace the passing of the userid from page one to page three. only page two can receive the userid with either the _SESSION or the _POST method. but not the third page.
Can anybody tell me what I should do?
by the way, in the third page, I used the _SESSION['userid'] to try to extract the value. but it doesn't work,
:roll: :x

Thaks for your help!!

john
fielding
New php-forum User
New php-forum User
 
Posts: 8
Joined: Fri Oct 25, 2002 1:16 pm

Postby DoppyNL » Mon Nov 04, 2002 12:16 am

When using session-variables you must you "session_start()" at the beginnen of your script to retrieve the session data and make it available in your script.

as for passing the userid with the post-method, that's not very secure. It is very easy for someone to change that user id and then he is able to do things on some else's account.

best is to use the session to store user information.

Greetz Daan
DoppyNL
 

Postby Jay » Mon Nov 04, 2002 12:20 am

Couple of things:

session_register($user_id); is wrong. You don't register the variable, you just register the name of the variable. After that you can consider it to be in the session, so register it first:
Eg session_register('user_id');
$user_id = whatever;

Second, if you've got the SGA enabled, it's easier to just use $_SESSION['user_id']. You don't need to explicitely register it (because PHP now takes care of that for you), just make sure to start the session first.

BTW, if you want to pass a variable from page 1 to 3, use either a session or cookie, or a variety of GET and POST methods in links embedded in the second page.
Jay
 

Postby fielding » Mon Nov 04, 2002 6:34 am

Thanks for you two guys' reply, but I still don't get through my problem.
actually, I am just learning PHP, I have very vouge idea of session register. in my impression, it seems that
-- once we have the session_start() started (if the register_globals is set off, mine is off), ---> session_start();

-- define a varible -->$VAR;

-- has it session registered --> session_register("VAR")

-- in the following script assign any value to it by anyway--->$VAR = 10,
or only if the variable name VAR is used in a form etc..;

then the value of var can be extracted by _SESSION['VAR'] anywhere, anytime as long as the session is on, right?



if so, could u give me a definition of session?
say, I have a web sit consisting 10 web pages, if I start my web site (starting from the first page), then the session starts, right?

if I define and session registered a variable VAR2 at page two, and have it assigned at page two, then I can retrieve it at page 6, page 9, right?

thanks again,

John
fielding
New php-forum User
New php-forum User
 
Posts: 8
Joined: Fri Oct 25, 2002 1:16 pm

Postby Jay » Mon Nov 04, 2002 7:13 am

The easiest way to use a session is to start the session as normal (session_start()) and then use $_SESSION['varname'] for all the variables as normal. Then you don't need to mess about with registering them etc
Jay
 

Postby DoppyNL » Mon Nov 04, 2002 7:20 am

simply use session_start() on all the pages you want to use session variables.

if you want to create a new one, just do something like this:
Code: Select all
$_SESSION['myvar'] = 12;

you can then acces that variable in all pages you open after that via $_SESSION['myvar'].

If you have no need for a variable anymore you can remove it by using "unset($_SESSION);"

Greetz Daan
DoppyNL
 

Postby Jay » Mon Nov 04, 2002 7:21 am

DoppyNL wrote:If you have no need for a variable anymore you can remove it by using "unset($_SESSION);"

Greetz Daan

Or you can use unset($_SESSION['myvar']) to get rid of a specific variable.
Jay
 

Postby DoppyNL » Mon Nov 04, 2002 7:23 am

Jay wrote:Or you can use unset($_SESSION['myvar']) to get rid of a specific variable.

Woops, Thats what I meant to say...
my mistake

Greetz Daan
DoppyNL
 

Postby fielding » Mon Nov 04, 2002 8:09 am

DoppyNL wrote:simply use session_start() on all the pages you want to use session variables.

if you want to create a new one, just do something like this:
Code: Select all
$_SESSION['myvar'] = 12;

you can then acces that variable in all pages you open after that via $_SESSION['myvar'].



I am sorry, I still can't have it passed to next page.
to make thing clear:
1. on the first page, I enter userid, I will use this userid at page (say) 3, or 7 whatever.
2. I typed <?php
session_start();
session_register("userid");
?>
at the top of page ONE.
3. in the form of page ONE, I put the variable 'userid' in the input name of user ID
<input type ="text" name = "userid'>,
then a button to submit
4. on the second and third page (also at the top of the pages), I tried to extract the userid by the same way as:
<?php
session_start();
$get_var = $_SESSION['userid'];
echo "show me the userid: $get_var";
?>

the echo just display nothing! :cry: :cry:

I guess the input value (userid) is not attached to the varible defined in the session_register ( 'userid')!
fielding
New php-forum User
New php-forum User
 
Posts: 8
Joined: Fri Oct 25, 2002 1:16 pm

Postby DoppyNL » Mon Nov 04, 2002 8:26 am

indeed, it is going a little wrong, you should do it like this:

- page 1:
just the login form, your not storing user info here yet because you don't know it yet.

- page 2:
start your session.
use the posted data to check if the user has got enough acces.
store his ID in $_SESSION['userid'] ("$_SESSION['userid'] = $_POST['userid'];")

- page 3->whatever:
start your session.
acces $_SESSION['userid'] to get the users id.


use session_start(); when you want to acces variables previously stored in a session. or when you want to store variables in a session. You also want to use it at the beginning of the script as it tries to send a cookie.

session_register is no longer needed. Just store the variable directly into the array $_SESSION and php will do that for you.

Greetz Daan
DoppyNL
 

Postby fielding » Mon Nov 04, 2002 10:25 am

thanks, DoppyNL,

yes, if I start the session at page two, the userid can be accessed at page two. but still, it is not availble at page three! :x

1. on page two, I use:
Code: Select all
<?php session_start();
         $_SESSION['user_id'] = $_POST['user_id'];
     $id = $_SESSION['user_id'];

   echo "show me userid at page2: $id";
  ?>

( of course, I must check the user access right,)

I can see the userid here.
2. on page 3. I use almost the same codes, except the _POST[]:
Code: Select all
<?php session_start();
           $id = $_SESSION['user_id'];

   echo "show me userid at page3: $id";
  ?>


but the userid doesn't appear :x :x

right now, I am doubting I probably didn't configure my PHP well?
or my PHP is not in the right version?, I just downloaded the PHP4 about one month ago.
fielding
New php-forum User
New php-forum User
 
Posts: 8
Joined: Fri Oct 25, 2002 1:16 pm

Postby Jay » Mon Nov 04, 2002 12:44 pm

First of all, stop trying to avoid using $_SESSION, you can't run away from it and it's bad practise. Try this:

Page 1
<form action='page2.php' method='post'>
<input type='text' name='name'>
<button type='submit' value='submit'>

Page 2
<?
session_start();
if (isset($_POST['name'])) {
$_SESSION['name'] = $_POST['name'];
print "Thankyou ".$_SESSION['name'];
print "<a href='page3.php'>Page 3</a>\n";
} else {
print "Something went wrong";
}
?>

Page 3
<?
session_start();
if (isset($_SESSION['name'])) {
print "hi ".$_SESSION['name'];
} else {
print "Doh";
}

Try that and see how you get on!
Jay
 

Postby fielding » Mon Nov 04, 2002 6:54 pm

Thank you Jay for your example codes. I tried, but the result is a little disappointed:
I enter John, on the second page, i was greeted: thank you John.
but on the page3, I should be greeted again by: Hi, john. but instead, only Doh occured. :oops:

anyway, thanks for you two guys' help, now I am using the hidden input tag method doing that, as Jay reminded, that is not secure enough, but so far, I have no good idea.

if you guys have a good solution, pls post here! :wink:

John
fielding
New php-forum User
New php-forum User
 
Posts: 8
Joined: Fri Oct 25, 2002 1:16 pm

Postby Jay » Tue Nov 05, 2002 6:16 am

use print_r($_SESSION) on both pages (2 and 3) just after you start the session and see what that outputs. Also, check your browser is accepting cookies.
Jay
 

Postby fielding » Wed Nov 06, 2002 10:22 pm

it seems that my PHP doesn't recognize the print_r(); function. actually, I always use echo, the print() function also works.
the problem is the same. :cry:

john
fielding
New php-forum User
New php-forum User
 
Posts: 8
Joined: Fri Oct 25, 2002 1:16 pm


Return to PHP coding => General

Who is online

Users browsing this forum: yetticaretti and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.