berry05 Posted December 12, 2008 Share Posted December 12, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/ Share on other sites More sharing options...
Flames Posted December 12, 2008 Share Posted December 12, 2008 $_SESSION['user'] = username; $_SESSION['gold'] = gold; hope that helps Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714173 Share on other sites More sharing options...
gevans Posted December 12, 2008 Share Posted December 12, 2008 when you set the username $_SESSION['username'] = whatever also set a gold one $_SESSION['gold'] = whatever Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714176 Share on other sites More sharing options...
berry05 Posted December 12, 2008 Author Share Posted December 12, 2008 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; Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714183 Share on other sites More sharing options...
Flames Posted December 12, 2008 Share Posted December 12, 2008 why are you selecting gold if you already know what the gold is? Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714190 Share on other sites More sharing options...
berry05 Posted December 12, 2008 Author Share Posted December 12, 2008 should i put a * instead then? Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714202 Share on other sites More sharing options...
Flames Posted December 12, 2008 Share Posted December 12, 2008 from the looks of it you dont need a query at all, can you post the whole code please? Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714216 Share on other sites More sharing options...
berry05 Posted December 13, 2008 Author Share Posted December 13, 2008 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'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714311 Share on other sites More sharing options...
premiso Posted December 13, 2008 Share Posted December 13, 2008 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']; Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714334 Share on other sites More sharing options...
berry05 Posted December 13, 2008 Author Share Posted December 13, 2008 i tried the code and now i get no errors in the script but the gold doesnt show up on the index2.php page... ??? Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714394 Share on other sites More sharing options...
premiso Posted December 13, 2008 Share Posted December 13, 2008 Are you using session_start on both pages? Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714432 Share on other sites More sharing options...
Flames Posted December 13, 2008 Share Posted December 13, 2008 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']; Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714461 Share on other sites More sharing options...
berry05 Posted December 13, 2008 Author Share Posted December 13, 2008 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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714602 Share on other sites More sharing options...
berry05 Posted December 13, 2008 Author Share Posted December 13, 2008 i checked everything and i dont know why its not working... Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714663 Share on other sites More sharing options...
.josh Posted December 13, 2008 Share Posted December 13, 2008 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' Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714673 Share on other sites More sharing options...
berry05 Posted December 14, 2008 Author Share Posted December 14, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-714968 Share on other sites More sharing options...
Flames Posted December 14, 2008 Share Posted December 14, 2008 try setting $_SESSION['gold'] to 1234 and then test it. Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-715139 Share on other sites More sharing options...
sasa Posted December 14, 2008 Share Posted December 14, 2008 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"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-715182 Share on other sites More sharing options...
berry05 Posted December 15, 2008 Author Share Posted December 15, 2008 it worked!! thank you!! i also got rid of table 2 and had gold in the first table where the username was...i think that was messing it up to.. Quote Link to comment https://forums.phpfreaks.com/topic/136744-solved-is-there-a-way-i-can-have-more-than-one-session/#findComment-715529 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.