Connecting Tech Pros Worldwide Forums | Help | Site Map

Sessions trouble

Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#1: Jul 27 '07
I'm using Jpmaster77 a.k.a. The Grandmaster of C++ 's register/login system and I'm having a problem with the session being dropped when I try to submit a form to the same page.

A user has to register and login to access the form. The information on the form is suppose to be entered into a database and the user given an all OK message, with the user options shown at the top.

The form I'm using is a copy of a working form with only the database info changed. However, the information isn't being entered into the database and when the page is displayed again it says I have to login.

So I don't know if whether the form is interfering with the session or the session with the form.

This register/login system uses a sessions.php page that you include at the top of the pages you want the session information added to. Here's the scenario:
[PHP]
<?php
include("include/session.php");
?>

<?php
include('functions.php');
head();
?>

<?php
// begin session login check
if($session->logged_in){
echo "
<strong>$session->username</strong>, you are logged in.<br>
$session->company<br>
$session->email<br>
$session->address<br>
$session->city, $session->state $session->zip<br><br>

[<a href=\"userinfo.php?user=$session->username\">My Account</a>] &nbsp;&nbsp;
[<a href=\"useredit.php\">Edit Account</a>] &nbsp;&nbsp;
[<a href=\"process.php\">Logout</a>]<br><br><hr>
";

// process form results
submitted form process section

?>
<form>
Field 1
Field 2
</form>

<?php

} else {

echo "You must <a href='main.php'>Login</a> to access this page.";

}
?>

<?php
footer();
?>
[/PHP]

volectricity's Avatar
Expert
 
Join Date: Jun 2007
Location: Baltimore
Posts: 587
#2: Jul 27 '07

re: Sessions trouble


How is your session class structured? Does the constructor call the session_start() method on it's on? And does it use __get() and __set() to handle the session variables?
Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#3: Jul 27 '07

re: Sessions trouble


The sessions.php has this:
[PHP] /**
* startSession - Performs all the actions necessary to
* initialize this session object. Tries to determine if the
* the user has logged in already, and sets the variables
* accordingly. Also takes advantage of this page load to
* update the active visitors tables.
*/
function startSession(){
global $database; //The database connection
session_start(); //Tell PHP to start the session

/* Determine if user is logged in */
$this->logged_in = $this->checkLogin();[/PHP]

I'm using this in my form:
[PHP]<?php
$username = ""; //Username, default as blank
if (isset($_SESSION['username'])) $username = $_SESSION['username'];
?>
<input type="hidden" name="username" value="<?php echo $username ;?>">[/PHP]
Could this be causing my form to not enter information into the database? It's the only thing that I'm doing on this form that is different from what I normally do.

***
OK, I changed teh above to this:
[PHP]<input type="hidden" name="username" value="<?php echo $session->username ;?>">[/PHP] and eliminated some of the user session information. Now when I submit the form I don't get dropped from sessions. But my form still shows with some of the information I entered into it when I submit it, and the information is still not going into the database.

But it's different from before so it's progress.
Familiar Sight
 
Join Date: Mar 2007
Posts: 146
#4: Jul 27 '07

re: Sessions trouble


I created a separate page to handle my form process and I included the sessions on it. Information will not enter into the database, so the sessions must be causing the problem.

But if I leave it off, will it cause the sessions to break when they return to the main page?
dafodil's Avatar
Needs Regular Fix
 
Join Date: Jul 2007
Location: Philippines
Posts: 393
#5: Jul 27 '07

re: Sessions trouble


Check if you are calling this function:
startSession();

Check the datas you are storing to your session variables:
It should go something like this:
$_SESSION['username']=$usernamevalue;
Reply