简体   繁体   中英

PHP Session variable problems

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>";
  }
?>

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM