Jump to content

Passing session from one page to another


pkmleo

Recommended Posts

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.

Link to comment
Share on other sites

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

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

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.
Link to comment
Share on other sites

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

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

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

echo $_SESSION['test'];

?>[/code] Upload both files.And run test.php and click the [b]Test session[/b] link. It should take you to test2.php and hello world should be displayed.
Link to comment
Share on other sites

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.
[code]
<?php
session_start();

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

Now create a file called page2.php with this code...
[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";
}
?>
[/code]
Link to comment
Share on other sites

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"

Link to comment
Share on other sites

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
[code]<?php

session_start();

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

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

test2.php
[code]<?php
session_start();

echo $hello;

?>[/code]
Also make sure your browser(s) accepts cookies!

Do you get a result this time?
Link to comment
Share on other sites

[!--quoteo(post=377011:date=May 25 2006, 08:15 AM:name=pkmleo)--][div class=\'quotetop\']QUOTE(pkmleo @ May 25 2006, 08:15 AM) [snapback]377011[/snapback][/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.
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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: [code]<? session_start()?>[/code]

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.