How create SESSION with dynamically name?

这一生的挚爱 提交于 2019-12-25 18:41:48

问题


The Example code: I have variable $getname that get informations from the url GET

$getname=$_GET['id'];

If it possible to store session like this, cause it doesn't work for me.

$_SESSION[$getname]=$_GET['id'];

Because i need to create for every id individually session with the same values.

PROBLEM SOLVED: $getname='name'_$_GET['id']; //cause $_GET['id'] was a number, that couldn't be the session name.

$_SESSION[$getname]=$getname;


回答1:


You can do this way..

$getname=$_GET['id'];
$_SESSION[$getname]=$getname;

So you can access like

echo $_SESSION[$getname]; //Prints what you really had in the $_GET['id'] variable from the URL

A simple example..

<?php
session_start();
$getname="hello";
$_SESSION[$getname]=$getname;
echo $_SESSION[$getname];

OUTPUT:

hello



回答2:


The $_SESSION would be the other way round :

        $getinput = $_GET['id'];
        $_SESSION['name your session here']=$getinput

;




回答3:


Why create an array element that has the exact same key as a value? Just create an array of the IDs that can iterate over later:

$_SESSION['needed_ids'][] = $_GET['id']

Add some checks to cleanly create the array first, and filter the incoming data.



来源:https://stackoverflow.com/questions/19879839/how-create-session-with-dynamically-name

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!