Jump to content


Photo

SESSIONS DONT WORK


  • Please log in to reply
3 replies to this topic

#1 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 12 July 2006 - 04:50 PM

ok ill show the process

first is Check User;
<?
/* Check User Script */
session_start();  // Start Session

include 'db.php';
// Conver to simple variables
$username = $_POST['username'];
$password = $_POST['password'];

if((!$username) || (!$password)){
	echo "Please enter ALL of the information! <br />";
	include 'login_form.html';
	exit();
}

// Convert password to md5 hash
$password = md5($password);

// check if the user info validates the db
$sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");
$login_check = mysql_num_rows($sql);

if($login_check > 0){
	while($row = mysql_fetch_array($sql)){
	foreach( $row AS $key => $val ){
		$$key = stripslashes( $val );
	}
		// Register some session variables!
		session_register('first_name');
		$_SESSION['first_name'] = $first_name;
		session_register('last_name');
		$_SESSION['last_name'] = $last_name;
		session_register('email_address');
		$_SESSION['email_address'] = $email_address;
		session_register('special_user');
		$_SESSION['user_level'] = $user_level;
		session_register('userid');
		$_SESSION['userid'] = $userid;
		session_register('username');
		$_SESSION['username'] = $username;
		
		mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'");
		
		header("Location: header.php");
	}
} else {
	echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br />
	Please try again!<br />";
	include 'login_form.html';
}
?>

this is from phpfreaks i added two additional register session handles userid and username.

this takes u to

header.php : simple menu selection

<?php
session_start();

echo "Welcome Commander". $_SESSION['username'] ." to you empire.";

echo "Manage your kingdom:";
echo"<a href='buildings.php>Buildings</a>";
echo"<a href='research.php>Research</a>";
echo"<a href='shipyard.php>Shipyard</a>";

?>



so to the main part of site: the session should follow and maintain userid and username right?

<?php
session_start();
//buildings.php

//open database connections
require_once('db.php');

if ($_POST['farm'] || $_POST['house'] || $_POST['mine'])
{
 $farm = $_POST['farm'] > 0 ? $_POST['farm'] : 0;
 $house = $_POST['house'] > 0 ? $_POST['house'] : 0;
 $mine = $_POST['mine'] > 0 ? $_POST['mine'] : 0;
 $qry = "update buildings set nfarms = nfarms + " . $farm . " where userid='$userid', nhouse = nhouse + " . $house . " where userid='$userid', nfarm   = nfarm +  " . $farm . " where userid='$userid'";
 $qry = mysql_query($qry);
}

//define planet size
define("TLAND", 200);
//get current number of buildings
$qry="select nfarms,nhomes,nmines from buildings where userid='$userid'";

$qry = mysql_query($qry);
$row = mysql_fetch_assoc($qry);
$nf = $row[nfarms];
$nh = $row[nhomes];
$nm = $row[nmines];

//check for free land sapce
$fland = ((TLAND) - ($nf+$nh+$nm));

//output current data
echo "Total land: " . TLAND;
echo "Free land: " . $fland;
echo "Number of Farms: " . $nf;
echo "Number of Homes: " . $nh;
echo "Number of Mines: " . $nm;

//allow building form
if ($fland>0)
{
?>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<input size="4" type="text" name="farm">
<input size="4" type="text" name="house">
<input size="4" type="text" name='mine'>
<input type="submit" name="submit" value="Build">
<?php
}
else
{
echo "No free land to build upon";
}
?>

until finally they can logout (this again from php freaks)

<?
session_start();

if(!isset($_REQUEST['logmeout'])){
	echo "<center>Are you sure you want to logout?</center><br />";
	echo "<center><a href=logout.php?logmeout>Yes</a> | <a href=javascript:history.back()>No</a>";
} else {
	session_destroy();
	if(!session_is_registered('first_name')){
		echo "<center><font color=red><strong>You are now logged out!</strong></font></center><br />";
		echo "<center><strong>Login:</strong></center><br />";
		include 'login_form.html';
	}
}
?>

so why do my sessions not work for keeping userid and username for main section of site i.e. buildings.php.  is there some clash or am i going wrong somewhere.

#2 kenrbnsn

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

Posted 12 July 2006 - 05:00 PM

Remove all of the session_register() function calls.

Please read all of the cautions about this function at http://www.php.net/m...on-register.php

Ken

#3 Newbiephper

Newbiephper
  • Members
  • PipPip
  • Member
  • 21 posts

Posted 12 July 2006 - 05:10 PM

ok i read the page 2x over and say cautions talk of globals but wasnt understanding it just as i havent understood any of that manual. 

i read that since php 4 globals are off by default and will be completely removed  in php 6.

so maybe im missing something but what do i replace register session with/how do i solve my problem?

wow im more confused now than before :(

If your script uses session_register(), it will not work in environments where the PHP directive register_globals  is disabled.


If you want your script to work regardless of register_globals, you need to instead use the $_SESSION array as $_SESSION entries are automatically registered.


does this mean its automatic? i.e. i dont need to do anything other than delete the waste code (i.e. register session)?

i origianlly though the problem was with my code, particuarly bits like this
where userid='$userid'


#4 GingerRobot

GingerRobot
  • Staff Alumni
  • Advanced Member
  • 4,086 posts
  • LocationUK

Posted 12 July 2006 - 05:11 PM

indeed, to declare a session simply do:
$_SESSION['sessioname'] = "Session value";




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users