【问题标题】:How can I get session data in PHP? [duplicate]如何在 PHP 中获取会话数据? [复制]
【发布时间】:2021-09-13 21:27:05
【问题描述】:

我是 PHP 新手,所以这是我的问题

index.php

 if(!function_exists("session_register")) {  
    function session_register() {  
        $args = func_get_args();  
        if(count($args) <= 0)  
        return;  
        foreach($args as $key)  
            $_SESSION[$key] = $GLOBALS[$key];        
    }  
} 
include("connection2.php");
ob_start();
session_start();



if($_SERVER["REQUEST_METHOD"] == "POST") {       
    
    $myusername = mysqli_real_escape_string($db,$_POST['username']);     
    $mypassword = mysqli_real_escape_string($db,$_POST['pass']);     
    $sql = "SELECT * FROM Users WHERE UserName='$myusername' and UserPassword='$mypassword'";   
    $result = mysqli_query($db,$sql);   
    $row = mysqli_fetch_array($result);
    $userID = $row["UserID"];  
    $userType = $row["UserType"];  
    
    
    $count = mysqli_num_rows($result);             
    if($count >0 ) {
        if($userType == 2 || $userType == 1 ) { 
            $_SESSION['UserID']=$userID;
            $_SESSION['UserType']=$userType;
            header("location: dashboard.php");
            ob_end_flush();
            
        } else {
            
            
             header("location:index.php");
             ob_end_flush();
        }
    } 
} 

session.php

<?php
   include('connection2.php');
   session_start();
   
   $userID =  $_SESSION['UserID'];
   $userType =  $_SESSION['UserType'];
   $ses_sql = mysqli_query($db,"select * from Users where UserID = $userid ");
   
   $row = mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);
   
   $user_info = $row['UserInfo'];
   
   if(!isset($_SESSION['UserID'])){
      header("location:index.php");
      die();
   }
?>

dashboard.php

<?php 
include("session.php");

    if($userType == 1){
        include("_header.php");
    }else {
        include("_header2.php");
    }
    
   
?>

我的代码总是重定向到 index.php 但我可以看到我的变量 /* $userID = $row["UserID"]; / and / $userType = $row["UserType"];*/ in index.php

如何在dashboard.php上传递我的变量,无论是否使用会话。

我需要帮助。非常感谢。

【问题讨论】:

  • session_register() 早就被删除了

标签: php session session-variables


【解决方案1】:

您可以通过查询参数传递它。这样你就可以写了,

header('location: dashboard.php?userId=' . $userID);

然后在dashboard.php里面,你可以使用$_GET['userId']访问用户的id。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-05-28
  • 2021-12-31
  • 2021-09-29
  • 1970-01-01
  • 2016-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多