How to set and access session after using session_name

Asked

Viewed 1,069 times

1

I have a $_SESSION["id"]="123"; and direct access her so:

echo $_SESSION["id"]; 

If I use the

session_name(md5("seg".$_SERVER["REMODE_ADDR"].$_SERVER["HTTP_USER_AGENT"]));

how I will access or set the session?

1 answer

1


If you only use the session_name() it will return the current session name. When you create a name for the session_name($name) it will overwrite the current session name and return the old name.

So when you give a name to the session:

session_name(md5("seg".$_SERVER["REMODE_ADDR"].$_SERVER["HTTP_USER_AGENT"]));

And you want to rescue her. Just do this:

$nomeDaSessao = session_name();

Take a look at the "manuel"

The session name refers to the session name, which is used in cookies and Urls (e.g., PHPSESSID). It must contain only characters alphanumeric; it should be short and descriptive (for users with cookie notices enabled). If name is given, the session name is modified to the new value.

EDIT

page1.php

// a variável ficará com o nome da antiga sessao PHPSESSID 
$sessao_antiga = session_name("teste"); // alterei o PHPSESSID para teste 

session_start(); // inicio a sessao

$session_name_new = session_name(); // recupero o nome da atual

echo "O nome da sessão é ".$session_name_new."<br>"; // mostro o nome da atual
echo "Mas o nome da sessão antes era ".$sessao_antiga."<br>"; // mostro o nome da antiga

$_SESSION['id'] = "123"; // abro uma sessão com o nome id

if(isset($_SESSION['id'])){ // verifico se existe a sessao
    echo "A sessão com a chave 'id' existe! O valor dela é ".$_SESSION['id']; // mostro uma msg
}

The above example will return:

O nome da sessão é teste
Mas o nome da sessão antes era PHPSESSID
A sessão com a chave 'id' existe! O valor dela é 123

Note that when you change the session name, you change the PHPSESSID cookie, and link the other sessions to this name. It is important to note that this change has to be made BEFORE the session_start().

However, for you to recover these keys in each script, you will need to change the session name also on the other pages. So:

page2.php

session_name("teste");

session_start();

if(isset($_SESSION['id'])){
    echo "A sessão 'id' Existe!";
}

The above example will return:

The id session Exists!

  • Thank you Andrei

  • @Rodrigomarques does not forget to approve... =) Hug!

  • Doubt, when using md5 to set a session name, there may be a problem if two users have the same session name?

  • @Guilhermecostamilam in this case, I don’t think so, because he’s taking the $_SERVER["REMODE_ADDR"]. But it would be nice to insert a date as well. And there’s a salt there with the "mon"

  • @Guilhermecostamilam if two users have the same session name, it won’t be a problem if he’s not managing it. Usually this name is changed so that you can manage it by having many users using the SAME application for example. Then you can take down the sessions dynamically.

  • @Guilhermecostamilam in his case is only for "security". I don’t even know if it enhances security. But... If it doesn’t enlarge, it doesn’t modify anything. It’ll just slow you down a little bit.

  • then I would enter $nameSessao['id']='blablabla'; and access thus echo $nameSssao['id']; because the name of the default section is PHPSESSID and I access with $_SESSION

  • @Rodrigomarques I will put an example for you to understand.

  • @Rodrigomarques became clearer? Something calls here...

  • from what I understand, regardless of the name of the section I will always use the $_SESSION['*'] to call it, the only difference is that I will have to set the name of the session before session_start(); of all my pages that use the session. that?

  • Is there any Pv here?

  • @Exact Rodrigomarques. If you do not set the name, it will not find the SESSION['*'].

  • @Rodrigomarques as well.. Pv???

Show 8 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.