PHP Sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

PHP Sessions

Post by farid »

Hi!!

I have a program that in the first page has a text box, and a submit button,in the second page I call the text box called var in PHP and I make it a Variable session, and in the third page I call such variable, but it says it is undefined, the session_start() is already put in the first page. The funny thing is that in works only for some computers.

CODE first page:

Code: Select all

<form name="form1" method="post" action="2.php">
  <?php 
print_r($_SESSION);
?>
  <p> 
    <input name="var" type="text" id="var" size="20" maxlength="20">
  </p>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
2 page:

Code: Select all

<form name="form1" method="post" action="3.php">
  <?php 
$_SESSION['var']=$_POST['var'];
echo "pag inter", "<br>";
print_r($_SESSION);
?>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
3 page:

Code: Select all

<form name="form1" method="post" action="1.php">
  <?php 
echo $var=$_SESSION['var'];
print_r($_SESSION);
?>
  <p>
    <input type="submit" name="Submit" value="Submit">
  </p>
</form>
Hope someone can help me!! Thanks in advance!!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

You need to call session_start in page2 and page3 too...
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

yeah, it is called also in such pages, sorry I did not explain that :oops:
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

Ok, I do not know what happened but, the session works now for only the second page, it appears on the

Code: Select all

print_r ($_SESSION);
But the Array is empty on the third page

:cry:
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Try changing

Code: Select all

echo $var=$_SESSION['var'];
to

Code: Select all

$var=$_SESSION['var'];
echo $var;
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

Nope, It does not works. The message that appears is:

Notice: Undefined index: var in D:\inetpub\InetGREGION9\php\fichas\3.php on line 10
Array ( )
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

So var is being set, its just that $array[$var] doesn't exist? In which case your sessions are working fine.

Or wait, was "line 10" the print_r($_SESSION); ?




Suggestion to admins: Make a way for php tags to start counting lines at a specified #
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

jshpro2 wrote: Suggestion to admins: Make a way for php tags to start counting lines at a specified #
There is
viewtopic.php?t=21171

Just nobody uses it...

Code: Select all

echo &quote; starting from line 10&quote;;
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

mmm, never read past feyd's first post in that topic. Thanks
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

Ok, the thing is that the variable: $var says that does not exist, so when I write the print_r ($_SESSION) it prints the values of such session, which is an array, so that is why it says Array ().
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

mm ok so it IS the session like you said, mind posting the entire code for page 2 and 3 ?
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

done it, if I do it like that it do works, is like the $_SESSION is the same thing as the $_POST, which of course is totally different but it does not behaves like that. :oops: :cry:
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

But it stills does not works the session, and I must make it work, 'cause a system that I have done of about 40 web pages of php depend on sessions.

please heeeelp!!!
User avatar
farid
Forum Commoner
Posts: 54
Joined: Thu Nov 11, 2004 4:20 pm
Location: Mexico

Post by farid »

someone heeeeeeeeeeeelp!! Do somebody knows if it has to do with a problem in the php ini file??
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

farid wrote:Ok, the thing is that the variable: $var says that does not exist, so when I write the print_r ($_SESSION) it prints the values of such session, which is an array, so that is why it says Array ().
If it only outputs Array() that means you have an empty array, thus your session vars were not set or your session was not started on that page.

By the way, please make use of the edit button instead of posting 3 posts in a row :P
Post Reply