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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?