Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    May 2008
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts

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

    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!

    index2.php

    Code:
    <?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

    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
    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();
    ?>

  • #2
    New Coder
    Join Date
    Jul 2008
    Location
    New York
    Posts
    24
    Thanks
    0
    Thanked 2 Times in 2 Posts
    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.

  • #3
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    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. Specifically, session_register(), 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.

  • #4
    New Coder
    Join Date
    May 2008
    Posts
    58
    Thanks
    3
    Thanked 0 Times in 0 Posts
    tried it...thats not it...i keep trying different things and the gold just isnt showing up ....

  • #5
    Senior Coder
    Join Date
    Apr 2007
    Location
    Quakertown PA USA
    Posts
    1,028
    Thanks
    1
    Thanked 125 Times in 123 Posts
    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.

    Also:
    Code:
    if($field == 0)
    {
        //show nothing
    }else{
        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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •