Jump to content

Archived

This topic is now archived and is closed to further replies.

Newbiephper

SESSIONS DONT WORK

Recommended Posts

ok ill show the process

first is Check User;
[code]
<?
/* 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';
}
?>
[/code]

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

this takes u to

header.php : simple menu selection

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

?>


[/code]

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

[code]
<?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";
}
?>
[/code]

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

[code]
<?
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';
}
}
?>
[/code]

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.

Share this post


Link to post
Share on other sites
Remove all of the session_register() function calls.

Please read all of the cautions about this function at http://www.php.net/manual/en/function.session-register.php

Ken

Share this post


Link to post
Share on other sites
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 :(

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

[quote]
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.
[/quote]

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
[code]
where userid='$userid'
[/code]

Share this post


Link to post
Share on other sites
indeed, to declare a session simply do:
$_SESSION['sessioname'] = "Session value";

Share this post


Link to post
Share on other sites

×

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.