1

My issue I am encountering is that we are using PHP session variables for our project. The way that the variable is stored for username is that when the user logs in with the username that username is stored in a session variable. I need to get that username's id from the database and use it on another page from the user that is logged in. I can't even get the username to be printed out from the session variable. I have been struggling very much on this and have no idea where to go with this and I would greatly appreciate the help. I added comments on the second file. I also was not sure if I need to add session_start(); on every file that I want to use session variable? Once I gather a variable in one file can I access it on the the other file?

This file is the log in file:

<body>
<h1>Login</h1>
<form action="VerifyUser.php" method="post">
Username: <input type="text" name="userName"><br>
Password: <input type="password" name="pwd"><br><br>
<input type="submit" name="loginButton" value="Login"><br><br>
<a href="forgotPassword.php">
Forgot Password? Click Here!</a>
<a href="Registration.php"><br><br>
Need to Register? Click Here!</a>
</form>
</body>

This is the file it posts to on verify user: (When I log in the new page only prints "Working" from the the echo prior to the extract POST

<?php
    require_once('dbBaseDao.php');
    require_once('dbUserDao.php');
    $userDao = new dbUserDao();
    echo "<p>Working...</p>";
    extract($_POST);
    if( $userDao->{'verifyUser'}($userName, $pwd))
    {
        session_start();
        $_SESSION['userName'] = $userName;
        $result = $userDao->{'getUserByWhere'}("username ='" . $userName . "'");
        $user = mysql_fetch_array($result, MYSQL_ASSOC);

            print $userName;      //This is not printing

        $_SESSION['userId'] = $user['userId'];    //I think to get this I need 
//to query the database for whatever the username is here it is equal to that 
//in the database and get the userId from that, but I have no idea how to do this. 


        header('Refresh: 1;url=LoggedIn.php');
        exit();
    }
    else
    {
        header('Refresh: 1;url=LogIn.php');
        exit();
    }
?>

My code: This is where I need to get the userId so that when I use the INSERT TO query I can send the data based on what userId selected the game.

<?php include('header.php'); ?>

<?php

  require_once('dbBaseDao.php');
  require_once('dbUserDao.php');
  require_once('dbBotDao.php');

  $userDao = new dbUserDao();
  $botDao = new dbBotDao();

  session_start();
  $userID = $_SESSION['userId'];
  print $userID;


/*
* Description of code below: This code checks the name checkbox[] from the form in UpcomingGames.php for any
* selected games made by clicking on the checkbox and submitting the form. The first echo actually prints all
* the games selected by the user from UpcomingGames.php.
*/

 if(!empty($_POST['checkbox']))
  {
    foreach($_POST['checkbox'] as $check) 
    {
           echo "</br>";
           echo ($check);
           $userID = 2;

           $sql= "INSERT INTO UserPicks (userID, game) VALUES ('$userID','$check')";
           $result = mysql_query($sql);
           if (!$result) {
               $message  = 'Invalid query: ' . mysql_error() . "\n";
               $message .= 'Whole query: ' . $sql;
               die($message);
            }
    }
    echo "</br>";
    echo "</br>";
    echo "You predicted ";
    echo sizeof($_POST['checkbox']);
    echo " games to have upsets";
    echo "</br>";
    echo "</br>";
  }
?>
user2522055
  • 159
  • 2
  • 13
  • 1
    @user25220055: try ob_stat() method. – Pank Apr 15 '14 at 20:13
  • $user['userId']; You sound like you aren't sure what you should do with the result from the query may I suggest a var_dump($user); This will serve two purposes, one it displays all the data in the object so that you can see it's structure, etc. and you'll basically be getting the same effect as your "print" statement but with a lot more information. Print says you got in the method, var_dump says you got in the method and says what your data is (even if empty/null). – Culyx Apr 15 '14 at 20:21
  • @Culyx Yeah that is something I tested and I thought I would just get the userID automatically, but for me to get the userId i know before that line I need to match the username in the table and get the userId. So if I understand correct for var_dump($user) I put that after my Session_['userId'] so that I can get what it will return? – user2522055 Apr 15 '14 at 20:25
  • @Pank I looked on google for description of ob_stat() but did not find anything other than ob_start. What does the method do so I know where to use it? – user2522055 Apr 15 '14 at 20:26
  • You can place the var_dump directly under this line: $user = mysql_fetch_array($result, MYSQL_ASSOC); once you assign the $user variable a value from the SQL query your var_dump will display what ever is in that value – Culyx Apr 15 '14 at 20:28
  • @user2522055 : ob_start — Turn on output buffering. Refer this link: http://stackoverflow.com/questions/4497382/what-is-the-role-of-ob-start-in-here – Pank Apr 15 '14 at 20:38

1 Answers1

3

I also was not sure if I need to add session_start(); on every file that I want to use session variable?

Yes, you have to use session_start(); on every page you need to get access into those sessions.

Machavity
  • 30,841
  • 27
  • 92
  • 100
B_CooperA
  • 659
  • 1
  • 9
  • 28