Jump to content

[SOLVED] is there a way i can have more than one session?


berry05

Recommended Posts

i have a session for usernames so when a user logs in his username is shown and i'm trying to make it so when a user logs in they see there username and there gold...i have a table dedicated to two fields called username and gold...and another table called users where when suers register there information goers there including there username...is there a way i can do it?

i tried that..it wont work because i have two tables...one is to login users and one is to track usernames and there stats..so i have two tables set like this..

Table 1:

username

password

 

Table 2:

username

gold

 

so i need two query's which i have and its not working...i have this..

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

sorry it took me awhile to respond...here's my login code....

<?php
ob_start();
$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());
$count=mysql_num_rows($result);
if($count==1){

session_register("username");
$_SESSION['username'] = $username;

header("location:index2.php");
}
else {
echo "Wrong Username or Password";
}

//start up the gold

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

//end of start up gold
ob_end_flush();
?>

 

 

 

 

here's my index page that shows the users gold and stuff....

 

<?php
session_start();
if(isset($_SESSION['username'])){
?>

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

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

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

echo  $_SESSION['gold'];


?></p>
Inventory: 
<br />
<?php 
//connect
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
}else{
echo Hoe;
}

}else{
header('Location: login.html');
}
?>

session_register

 

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.

 

Best to just use

 

$_SESSION['username'] = $username;

 

And do not worry about use that session_Register function.

 

To fix your issue, you are not actually querying the gold:

 


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

 

That should be something like

 


$queryy = mysql_query("SELECT gold FROM user_stats WHERE username='$username' AND gold='$gold'") or die(mysql_error());
$row = mysql_fetch_assoc($queryy);
$_SESSION['gold'] = $row['gold'];

 

 

login page needs session_start(); $gold is a variable that doesnt seem to have been set so im guessing no results have been found so try this.

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

change to

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

omg everything is fine but it still wont show the users gold..no errors but it still wont show the users gold...

 

index2.php

 

<?php
session_start();
if(isset($_SESSION['username'])){
?>


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

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

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

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


?></p>
Inventory: 
<br />
<?php 
//connect
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
}else{
echo Hoe;
}

}else{
header('Location: login.html');
}
?>

 

 

 

insert2.php

 

<?php
ob_start();
$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());
$count=mysql_num_rows($result);
if($count==1){

session_register("username");
$_SESSION['username'] = $username;

header("location:index2.php");
}
else {
echo "Wrong Username or Password";
}

//start up the gold
session_start();

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

//end of start up gold
ob_end_flush();
?>

It makes no sense to specify a gold amount in your where clause, when you're trying to find out the user's gold in the first place.  You have 2 tables, one with username, password and one with username,gold

 

select godl from table2 where username='$username'

It makes no sense to specify a gold amount in your where clause, when you're trying to find out the user's gold in the first place.  You have 2 tables, one with username, password and one with username,gold

 

select godl from table2 where username='$username'

 

i did that too...still doesn't work...its weird because it sessions registers the users username in a different table and the gold registers in a different table

try

<?php
//ob_start();
$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());
$count=mysql_num_rows($result);
if($count==1){
session_start();
//session_register("username");
$_SESSION['username'] = $username;
$queryy = "SELECT gold FROM user_stats WHERE username='$username'";
$result1 = mysql_query($queryy);
$row = mysql_fetch_assoc($result1);
$_SESSION['gold'] = $row['gold'];

header("location:index2.php");
}
else {
echo "Wrong Username or Password";
}

?>

Archived

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

×
×
  • Create New...

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.