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