dongzhisang5342 2016-08-16 06:28
浏览 37
已采纳

从两个地方获取会话变量并设置为一个变量

I have this at the top of pages that I don't want people accessing unless they're logged in:

<?php
session_start();
if(!isset($_SESSION['Register'])){
  if(!isset($_SESSION['Login'])){
    header("location:../notLoggedIn.php");
 }
}
$get_id = $_SESSION['Register'];
$get_id = $_SESSION['Login'];
?>

This gets a session variable from the code that adds a new user to the database when a new user registers. It also checks if a session has been set from a user logging in rather than registering. Hence one check for register and one for login.

Then, I'm using the session variable as a regular variable so I can access that data on any other pages I might write for the website. So I can have the users id easily available to me.

This works fine when a user registers but not when a user logs in. So, when a user registers the id they are given in the database is available to me to use as it's set in this code that's at the top of every page which puts their id number into the variable $get_id.

The problem is that this variable is already being used but I want it to contain the users id when the user logs in as well. Is there a way to set the variable to check for either of one session variables?

  • 写回答

2条回答 默认 最新

  • duanpie2834 2016-08-16 06:44
    关注

    You could make a check:

    if(isset($_SESSION['Register'])){
       $get_id = $_SESSION['Register'];
    }else if(isset($_SESSION['Login'])){
       $get_id = $_SESSION['Login'];
    }
    

    With this, you have the ID of Register if this is set, or the ID of Login if Register is not set but Login is. You could also switch the Register and Login-Parts in the check, if you want to have Login if it is set and if you only want to have Register if Login is not set.

    But if you want both IDs saved to the same variable, you'd have to make an array:

    $get_id = array(
       "Register" => $_SESSION['Register'],
       "Login" => $_SESSION['Login']);
    

    Like this you can then access your Register by typing $get_id['Register'];.

    If you don't want named values, you can omit them, like this:

    $get_id = array($_SESSION['Register'], $_SESSION['Login']);
    

    Like this, you can access your Register by typing $get_id[0]; and your Login respectively by typing $get_id[1];.

    But if you want to save both values to one variable (not an array), I'm afraid, that's not possible. The closest you could do is the check mentioned above.

    Alternatively you could, if you're desperate enough, format those both SESSION-variables to strings to concat them together to one variable. But believe me, this would be very very painful to manipulate or use this data.

    $get_id = $_SESSION['Register']. " ".$_SESSION['Login'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!