FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Session problem





biljap
Hi!

I have a problem with sessions. When I try:
Code:
<?php
  $username = (string) $_POST['username'];
  session_start();
?>


and I set value of session I can see it on the page:
Code:
<?php
  $_SESSION[username] = $username;
  echo "username = ".$_SESSION[username];
?>


but when I try to get the value on some other page it doesn’t work.

How can I solve it?

Thanks.
gerpg
try using this...

Code:

<?php
Session_start();
$username = $_POST['username'];
session_register("username"); //register's variable 'username'
?>


Then on the request pages try...

Code:

<?php
Session_start();
  $_SESSION[username] = $username;
  echo "username: $username";
?>


If your'e still having problems then reply and i'll get back to you Wink


Louis.

Edit:
To tick 'Notify me when a reply is posted' box.
coeus
Yeah, you need the Session_start(); on every page.
gerpg
Thanks, i forgot to mention that.

the Session_start(); needs to be on every page you request the variable and its MUST appear before any html output otherwise you will get errors.


Louis.
vln004
biljap wrote:
Hi!

I have a problem with sessions. When I try:
Code:
<?php
  $username = (string) $_POST['username'];
  session_start();
?>


and I set value of session I can see it on the page:
Code:
<?php
  $_SESSION[username] = $username;
  echo "username = ".$_SESSION[username];
?>


but when I try to get the value on some other page it doesn’t work.

How can I solve it?

Thanks.


you're missing quotes on your indexes. should be $_SESSION['username']

why not just directly assign the variable?

Code:
<?php
  session_start();
  $_SESSION['username'] = $_POST['username'];

  echo '$_SESSION['username'] ='.$_SESSION['username'];
?>
biljap
Thank you for replies but it’s not working… I can see the values on the page where I assigned them but I can’t pass data to another page. Crying or Very sad
gerpg
Try using GET insted of POST. Might solve it depending on how you've setup your form to submit the variables..

Louis.
sonam
Quote:
Try using GET insted of POST. Might solve it depending on how you've setup your form to submit the variables..


GET and POST are not in correlation with sessions. Sessions are independent system of data handling.


First page:
Code:
<?php
session_start();
$_SESSION['username'] = $_POST['username']; 
?>


Second page:
Code:
<?php
session_start();
echo @$_SESSION['username'];
?>


Sonam
AOP Web Development
As what other suggest this code work's fine
Code:

<?
session_start();

session_register('username');

$_SESSION['username']=$_POST['username'];

echo "Your Username is : ".$_SESSION['username'];
?>


I think this code really work. Session is use as to stored temporary variable on every session if you wish to get the value into the other page all you need is to put a session_start(); on that certain page where you want to view or retrieve the value that you store in your session.
Related topics
an annoying problem - PLEASE help
Problem with session variables
SQL error notification in my forum
everybody has server 2 problem? [solved]
extremely short SESSION lifetime
not able to sustain session for long time
phpBB header problem.
dvd writer problem
Joomla Installation Problem --> Session Save Path
Problem with a script
How to send a save session
session not working behind router
problem with save session path in joomla 1.0.15 installation
Trying to install Joomla
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.