[SOLVED] PHP Sessions Not Carrying Over!

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
Mendon
Forum Newbie
Posts: 6
Joined: Wed Oct 06, 2004 12:39 pm

[SOLVED] PHP Sessions Not Carrying Over!

Post by Mendon »

Hello, I've just started working on some scripts using sessions, but the session variables AREN'T carrying over, any suggestions?

First file:

Code: Select all

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="form1" method="post" action="hee.php">
 <p> 
  <input name="textfield" type="text" id="textfield">
  Test, wee<br>
  <input name="textfield2" type="text" id="textfield2">
  <br>
  <input type="submit" name="Submit" value="Submit Test">
 </p>
 </form>
</body>
</html>
First PHP File:

Code: Select all

<?PHP
session_start(); 
include 'includes/dbconnect.inc.php'; 
  $name = $_POST['textfield'];
  $pass = $_POST['textfield2'];
$query="SELECT * FROM Mendon WHERE owner='$name' and count_id='$pass'";
$result=mysql_query($query);
$num_rows = mysql_num_rows($result);
if ($num_rows!= 0) {
  $_SESSION['name'] = $name; 
  $_SESSION['pass'] = $pass; 
global $_SESSION;
echo "Session made!".$_SESSION['name']."<a href='gah2.php'>Click here to test!</a>";
} else {
echo "Wrong info!";
}
?>
The session was viewable in that last file, the final PHP file:

Code: Select all

<?
session_start(); 
  $name = $_SESSION['name'];
  $pass = $_SESSION['pass'];
  $name2 = "Ha!";
  $nametest = $_SESSION['name'];
echo "Testing: ".$nametest." or ".$name2;
?>
The $name2 WON'T SHOW UP! So apparently the session isn't carrying over. >.< Any ideas?
Last edited by Mendon on Wed Oct 06, 2004 1:35 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

do a var_export($_SESSION); and set some debugging marks:

Code: Select all

<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
session_start();
var_export($_SESSION);
?>
try that as your "final" php file and see what get's dumped..

it may end up that where your sessions are being "stored" is getting wiped clear every few seconds. I've had one host do this on me without telling us.. I was extremely <span style='color:blue' title='I'm naughty, are you naughty?'>smurf</span> when I figured it out.. To fix this, I often move all my session handling to the database. I've used [php_man]session_set_save_handler[/php_man]() in the past for making the session handling just like normal.
Last edited by feyd on Wed Oct 06, 2004 12:52 pm, edited 1 time in total.
Mendon
Forum Newbie
Posts: 6
Joined: Wed Oct 06, 2004 12:39 pm

Post by Mendon »

Alright, I get back:

Warning: session_start(): open(/tmp/sess_5f9c9d07bc7eaf91e62a1a3bf23535ce, O_RDWR) failed: No such file or directory (2) in /mendon/gah2.php on line 4
array ( )
Warning: Unknown(): open(/tmp/sess_5f9c9d07bc7eaf91e62a1a3bf23535ce, O_RDWR) failed: No such file or directory (2) in Unknown on line 0

Warning: Unknown(): Failed to write session data (files). Please verify that the current setting of session.save_path is correct (/tmp) in Unknown on line 0

No idea what this means. :?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

that would tell me that your host's php.ini isn't set up correctly. If you have access to changing the ini, or want to set the save_path to an existing directory you have read and write access to, you can do that.. Personally, I'd move to using database sessions. :)
Last edited by feyd on Wed Oct 06, 2004 12:55 pm, edited 1 time in total.
Mendon
Forum Newbie
Posts: 6
Joined: Wed Oct 06, 2004 12:39 pm

Post by Mendon »

Great...so yahoo is stupid. >.> The host is yahoo, any idea how I can fix it though?

Here's a link to the PHPINFO() if that makes any difference.

http://www.thepokifiles.com/mendon/info.php

*Tilts head* And what are "Database sessions"?
Last edited by Mendon on Wed Oct 06, 2004 12:59 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

here's a previous post of an older database sessions thing I wrote.
Mendon
Forum Newbie
Posts: 6
Joined: Wed Oct 06, 2004 12:39 pm

Post by Mendon »

Wooahh...headache. >.< Mind shorthand explaining that to me?
Okay, how do I change the save_path to an existing directory locally? That will allow me to do sessions, so long as the /tmp is on MY server if I point it right, right?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

there's 1 class and 4 functions, plus some example/usage code.

function sessDataUnpack($sessData)
unpack a session data string. It will return an array of what the script saw, or would see.

function arrayPull(&$array, $key, $leave = false)
remove key-value pairs from the given array, returning the altered array and values extracted.

function sessDataPack($array)
pack up an array into a session string.

function sessDataRepack(&$sessionData, $keys, &$extracted, $leave = false)
unpack, extract, and package back up a given array for insertion into the database as a session string.

class session
a class containing the database connection information and functions needed to do processing of sessions for the php core.

Code: Select all

/* Create new object of class */ 
$ses_class = new session();

/* Change the save_handler to use the class functions */ 
session_set_save_handler (array(&amp;$ses_class, '_open'), 
                          array(&amp;$ses_class, '_close'), 
                          array(&amp;$ses_class, '_read'), 
                          array(&amp;$ses_class, '_write'), 
                          array(&amp;$ses_class, '_destroy'), 
                          array(&amp;$ses_class, '_gc'));
create an instance of the session class, and set the instance's methods as the session handling functions for this page instance.

Code: Select all

$_SESSION&#1111;'user'] = 'ted';
$_SESSION&#1111;'powerlevel'] = 20;
$_SESSION&#1111;'test'] = array('123',456,789=&gt;'012','345'=&gt;678);
demonstration of the scripts functionality
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

[php_man]session_save_path[/php_man]() can be used to set the path.
Mendon
Forum Newbie
Posts: 6
Joined: Wed Oct 06, 2004 12:39 pm

Post by Mendon »

Okay, session_save_path() worked GREAT! Besides one fact, won't that keep the sessions there 24/7? Is there a way so that it still clears it when they close the browser?
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

It won't be kept 24/7
Mendon
Forum Newbie
Posts: 6
Joined: Wed Oct 06, 2004 12:39 pm

Post by Mendon »

Draco_03 wrote:It won't be kept 24/7
Thanks again, I can use sessions! Now to try cookies..*Twitch*
Post Reply