Jump to content


Photo

Passing session from one page to another


  • Please log in to reply
16 replies to this topic

#1 pkmleo

pkmleo
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 24 May 2006 - 01:03 PM

Hi,

I am working on a small website. I am using PHP 4.3.4, Apache 1.2, MySQL 5.0 on Win XP Pro. The login script works properly but session is not passed on to the next page. My code looks like this:

<?php
session_start();
//header("Cache-control: private");
require_once($_SERVER['DOCUMENT_ROOT'].'/sports/includes/db_config.php');

$login_name = $_POST['login_name'];
$password = $_POST['password'];
$password = md5($password);
$userIP = isset($REMOTE_HOST) ? $REMOTE_HOST : @gethostbyname($REMOTE_ADDR);
$today = date("Y-m-d H:i:s");
$sql = "SELECT * FROM admin WHERE login_name='$login_name' AND password='$password'";
$query = "UPDATE admin SET last_login = '$today', ip_address = '$userIP' WHERE login_name = '$login_name'";
$result = mysql_query($sql) or die("Unable to execute $sql query: " . mysql_error());
$count = mysql_num_rows($result);
if($count == 1)
{
//$_SESSION['login_name'] = $login_name;
session_register('login_name');
$sessionId = session_id();
//print $_SESSION['login_name'];
//session_write_close();
//echo $_SESSION['login_name'];
header("Location: ./admin/adminhome.php?authenticateduser=$login_name?$sessionId");
//exit();
}
else
{
header("Location: relogin.php");
}
?>

I tried many options but still its not passing on the session to the next page. In the next page I am trying this:

<?php
session_start();
//if(isset($_SESSION['login_name']))
if(session_is_registered('login_name'))
{
//echo $_SESSION['login_name'];
echo '$login_name';
//header("location:../index.php");
}
?>

Its not printing the session. Any help with this will be appreciated.



#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 May 2006 - 01:10 PM

You are registering your sessions incorrectly. Scrap the sessin_register functions out and use this:
$_SESSION['login_name'] = $login_name;

Then on your other page use:
print $_SESSION['login_name'];

to get the login_name session.

Also your script wont output anythink as you was not assigning yout login_name session with nothing. So when you went to the next page nothing was outputted as the login_name session didn't have any assigned to it.

#3 pkmleo

pkmleo
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 24 May 2006 - 01:21 PM

Still its not working. Its not printing the session.


#4 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 May 2006 - 03:30 PM

Umm, OK create a new file called test.php and put the following into it
<?php
session_start();

$_SESSION['test'] = "Hello world";

?>
<a href="test2.php">Test session</a>
Now create a file called test2.php and put the following in it:
<?php
session_start();

echo $_SESSION['test'];

?>
Upload both files.And run test.php and click the Test session link. It should take you to test2.php and hello world should be displayed.

#5 pkmleo

pkmleo
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 25 May 2006 - 11:22 AM

No its not displaying Hello World in test2.php page

#6 Crimpage

Crimpage
  • Members
  • PipPipPip
  • Advanced Member
  • 70 posts
  • LocationBrisbane, Australia

Posted 25 May 2006 - 11:25 AM

Then your server is probably not creating the session files properly. Check the php.ini config, and make sure the tmp folder they are supposed to be written to has write access.

#7 pkmleo

pkmleo
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 25 May 2006 - 12:03 PM

I checked the tmp folder and I can see many session files being written in that folder. I dont understand this session thing in PHP. I did not experience this type of problems in JSP and ASP.


#8 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 25 May 2006 - 12:22 PM

If you follow this correctly, and your sessions are working as they should, you will click the link to go to page2 and will be greeted with a welcome message.

If the session is not working correctly, you will be told the session data didnt pass from the first page.


create a file called page1.php & put the following code into it.
<?php 
session_start();

$_SESSION[your_name] = "John Doe";
echo "$_SESSION[your_name]<br>";
echo "<a href=page2.php>Go to page 2</a>";
?>

Now create a file called page2.php with this code...
<?php
session_start();

if(!$_SESSION[your_name])
{
echo "The session variable did not pass from page1.php";
}
else
{
echo "Hello $_SESSION[your_name], welcome to page2.php";
}
?>

wisewood: proven fact, I am both wise, and wooden.

#9 pkmleo

pkmleo
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 25 May 2006 - 12:29 PM

I did it and I get the message that The session variable did not pass from page1.php. So in this what is the problem. But the session did display the value in page1.php, but not in page2.php

#10 wisewood

wisewood
  • Members
  • PipPipPip
  • Advanced Member
  • 226 posts
  • LocationRotherham, England

Posted 25 May 2006 - 12:49 PM

i know this is a stupid question, but you have got page1 and page2 on the same server haven't you? lol.

I cant be of any more help, but at least you know where the problem lies now.
wisewood: proven fact, I am both wise, and wooden.

#11 pkmleo

pkmleo
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 25 May 2006 - 12:54 PM

Yes both the pages are in the same page. Do I need to make any changes in the php.ini file. The php.ini file is below:

[Session]
; Handler used to store/retrieve data.
session.save_handler = files

; Argument passed to save_handler. In the case of files, this is the path
; where data files are stored. Note: Windows users have to change this
; variable in order to use PHP's session functions.
session.save_path = C:\tmp

; Whether to use cookies.
session.use_cookies = 1


; Name of the session (used as cookie name).
session.name = PHPSESSID

; Initialize session on request startup.
session.auto_start = 0

; Lifetime in seconds of cookie or, if 0, until browser is restarted.
session.cookie_lifetime = 0

; The path for which the cookie is valid.
session.cookie_path = /

; The domain for which the cookie is valid.
session.cookie_domain =

; Handler used to serialize data. php is the standard serializer of PHP.
session.serialize_handler = php

; Percentual probability that the 'garbage collection' process is started
; on every session initialization.
session.gc_probability = 1

; After this number of seconds, stored data will be seen as 'garbage' and
; cleaned up by the garbage collection process.
session.gc_maxlifetime = 1440

; Check HTTP Referer to invalidate externally stored URLs containing ids.
; HTTP_REFERER has to contain this substring for the session to be
; considered as valid.
session.referer_check =

; How many bytes to read from the file.
session.entropy_length = 0

; Specified here to create the session id.
session.entropy_file =

;session.entropy_length = 16

;session.entropy_file = /dev/urandom

; Set to {nocache,private,public} to determine HTTP caching aspects.
session.cache_limiter = nocache

; Document expires after n minutes.
session.cache_expire = 180

; trans sid support is disabled by default.
; Use of trans sid may risk your users security.
; Use this option with caution.
; - User may send URL contains active session ID
; to other person via. email/irc/etc.
; - URL that contains active session ID may be stored
; in publically accessible computer.
; - User may access your site with the same session ID
; always using URL stored in browser's history or bookmarks.
session.use_trans_sid = 0

url_rewriter.tags = "a=href,area=href,frame=src,input=src,form=fakeentry"



#12 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 25 May 2006 - 01:01 PM

Okay. Seems all this boils down one thing! You have register globals on. If you get a result from this then you have register globals on!
test.php
<?php

session_start();

session_register("hello");
$hello = "Hello world";

?>
<a href="test2.php">Test session</a>

test2.php
<?php
session_start();

echo $hello;

?>
Also make sure your browser(s) accepts cookies!

Do you get a result this time?

#13 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 25 May 2006 - 01:08 PM

One more question ...

Which web server are you using? If you're using IIS, I believe there are problems with IIS and sessions.

Ken

#14 pkmleo

pkmleo
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 25 May 2006 - 01:15 PM

Sorry, I am not getting the result. Its not displaying the session variable. I am not sure. Cookies are enabled in the browser.

I am using PHP 4.2.3-win32, Apache 1.3, MySQL 5.0 on Win XP Pro.

#15 chai

chai
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 26 May 2006 - 06:03 PM

[!--quoteo(post=377011:date=May 25 2006, 08:15 AM:name=pkmleo)--][div class=\'quotetop\']QUOTE(pkmleo @ May 25 2006, 08:15 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Sorry, I am not getting the result. Its not displaying the session variable. I am not sure. Cookies are enabled in the browser.

I am using PHP 4.2.3-win32, Apache 1.3, MySQL 5.0 on Win XP Pro.
[/quote]

I am facing the exact same problem. I've tried reinstalling apache server, suspecting it is the cause.

I have to revert back to a older version of phptriad before getting the session to work.
Now it is working for me. Well, at least I am able to pass session variables between pages.

Something is still notright though.

Maybe you can try installing other version of apache or try with other installer.

#16 pkmleo

pkmleo
  • Members
  • PipPip
  • Member
  • 12 posts

Posted 27 May 2006 - 10:42 AM

Still nothing has changed. I removed PHP 4.2.3 and Apache 1.3 and installed PHP 4.4.2 and Apache 2.2, still session is not getting passed to the next page. I changed the session.save_path in the php.ini file but its not saving the session in the folder. The old version of PHP which I was using used to save session in that folder. I even checked the folder properties its not read-only also. I dont understand why this is happenning.


#17 chai

chai
  • New Members
  • Pip
  • Newbie
  • 7 posts

Posted 28 May 2006 - 01:29 AM

well, also make sure that you have the statement session_start() at the very top(1st line) of each of your php pages. Even an empty space at the top of the page will result in that error.

so, just place:
<? session_start()?>

at the 1st line of your page.

This certainly solves my problem. Hope it does for you.
Maybe you'll want to have a look at the dedicated thread on "troubshooting session" in this forum.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users