簡體   English   中英

PHP Session變量問題

[英]PHP Session variable problems

我遇到的問題是我們正在為項目使用PHP會話變量。 為用戶名存儲變量的方式是,當用戶使用用戶名登錄會話變量時,用戶名登錄。 我需要從數據庫中獲取該用戶名的id,並在登錄用戶的另一個頁面上使用它。我甚至無法從會話變量中打印出用戶名。 我一直在努力奮斗,不知道該去哪里,我非常感謝你的幫助。 我在第二個文件上添加了評論。 我也不確定是否需要添加session_start(); 在每個我想使用會話變量的文件? 一旦我在一個文件中收集變量,我可以在另一個文件中訪問它嗎?

該文件是登錄文件:

<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>

這是它在驗證用戶時發布的文件:(當我登錄新頁面時,僅從提取POST之前的echo打印“Working”

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

我的代碼:這是我需要獲取userId的地方,這樣當我使用INSERT TO查詢時,我可以根據userId選擇游戲發送數據。

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

我也不確定是否需要添加session_start(); 在每個我想使用會話變量的文件?

是的,你必須使用session_start(); 在每個頁面上,您需要訪問這些會話。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM