View Full Version : is there a way i can have more than one session?

12-13-2008, 06:33 PM
everything is fine but it wont show the users gold..no errors but it just wont show the users gold...
I've been working on this for awhile to to get it working..

any help would be great!! thxs!



<p><a href="index2.php">Index</a> | <a href="shop.php">Shop</a> | <a href="logout.php">Logout</a></p>

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("textgame") or die(mysql_error());

echo " " . $_SESSION['username'];
<p>Gold: <?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("textgame") or die(mysql_error());

echo " " . $_SESSION['gold'];

<br />
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("register") or die(mysql_error());

$query = "SELECT item_name FROM items";

//0 or 1 situation
$field = ($row['item_name'] == 0) ? 0 : $row['hoe'];

if($field == 0){

//show nothing
echo Hoe;

header('Location: login.html');


$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="textgame"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$username = (isset($_POST['username'])) ? mysql_real_escape_string($_POST['username']) : FALSE;
$password1 = (isset($_POST['password1'])) ? $_POST['password1'] : FALSE;
$password2 = (isset($_POST['password2'])) ? $_POST['password2'] : FALSE;

if(!$username) die('username not set');
if(!$password1) die('Password1 not set');
else $password1 = md5($password1);
if(!$password2) die('Password2 not set');
else $password2 = md5($password2);

$query = "SELECT * FROM users WHERE username='$username' AND password1='$password1' AND password2='$password2'";
$result = mysql_query($query) or die( mysql_error());

$_SESSION['username'] = $username;

else {
echo "Wrong Username or Password";

//start up the gold

$queryy = "SELECT gold FROM user_stats WHERE username='$username'";
$row = mysql_fetch_assoc($queryy);
$_SESSION['gold'] = $row['gold'];

//end of start up gold

12-13-2008, 11:37 PM
Not entirely sure, but I'm looking at insert2.php and it looks like you redirect the user before the session variable 'gold' gets set? let me know if that helps.

12-14-2008, 01:37 AM
In insert2.php, you need to have session_start() at the top of your code.

Besides, what tnowalk noticed, prior to the header() statement, you set $_SESSION['username']. However, since the session has not been started, you really don't set anything. Therefore, when the code is redirected to index2.php, there is no session variable available to it.

I'd suggest you look at the manual regarding Sessions (http://us2.php.net/manual/en/book.session.php). Specifically, session_register() (http://us2.php.net/manual/en/function.session-register.php), and I quote:

Warning. This function has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

12-14-2008, 04:35 AM
tried it...thats not it...i keep trying different things and the gold just isnt showing up ....

12-14-2008, 06:01 AM
Well, I didn't mean that was the only problem, but it was certainly one of them.

Did you take heed of tnowalk's comments? Since the user is being redirected at the header() statement, the 'gold' is never being set and is therefore, not available in index2.php.

Frankly, your code is a bit of a mess. I can see where you are "trying different things", but not cleaning up after them. That's going to cause problems somewhere down the line. Twice in index2.php you establish a mysql connection and select a database, only to do nothing with them.


if($field == 0)
//show nothing
echo Hoe;

Unless Hoe has been defined as a constant, this will not function. In fact, had you proper error reporting / handling enabled, php would have told you this.

Also, ANY time you find yourself writing code like the above, where a condition results in something like "// show nothing", you really need to re-think the logic.

There may well be other problems, but with code like this you are really going to have to step through it, fixing one problem at a time.