Php: What Is the Purpose of Session_Name

PHP: what is the purpose of session_name

You have two sites on the same domain. (say, a blog and a forum)

They both run different pieces of software.

If they ran on the same session and used the same variables in $_SESSION, (say, user_id), they would conflict.

session_name lets you give each application a different session.

Using session_name() in PHP - Cannot Access Data

In light of nwolybug's post I think this must be due to some environmental settings. I can get this to work via doing the following:

if( $_COOKIE['fObj'] )
{
session_id( $_COOKIE['fObj'] );
session_start();
}
var_dump( $_SESSION );

Use of session_id() and session_name

The Session name is the name of the cookie/url param stores the session_id. By default when you create a session, a special cookie will be created normally with the name PHPSESSID, the session_id, is the value of that cookie that later identifies you. You can change the default session name by using session_name, but you must call session_name, before session_start. Hope that helps.

EDITED.

Session will send a cookie, to your browser, remember PHP is configured to use COOKIES as session management.

Once the cookie has been created and sent to the browser, then you can access it like any other cookie, if you print_R($_COOKIE), you will see the php session cookie and its value.

The the very minimum, all you need is a call to session_start() , this will start a session for you, it must be called, before any output data is sent to the browser.

For example, this will cause error.


echo "Hello";
session_start();

This is because session_start() must be called before any output to is sent to the browser.
So, why do you need session_name(), simply because it allows you to rename the cookie, as mentioned by default session name is PHPSESSID. Renaming the session lessens the chances of a hacker trying to find a cookie with the name PHPSESSID. Every php developer knows what the default session cookie name is, if I was a hacker, what cookie do you think I will look for first? But this is not a prevention mechanism.

The session_id(), is just to get the id of the session, it is not always used, it is there for convenience but also used if you trying to implement your own session management as PHP does allow you to do this.

In a nutshell, session_name or session_id, is not necessary to start a session. If you have further questions, you can follow me on my google page,

https://plus.google.com/113820735365251703271

and I will be happy to explain further.

What exactly is session_id( ) and session_name( )? Explain how they are being used in the following code

PHP uses cookies to manage sessions; specifically, by setting an identifying key/value pair for that session inside a cookie.

  • The name of the session is the name of the cookie; the default name for PHP-based websites is PHPSESSID. session_name() returns the session name or, if a parameter is passed, updates the session name.
  • The key/value pair inside the cookie describes the session id; the key denotes that it is the session identifier, and the value is the session identifier itself. session_id() returns the session id or, if a parameter is passed, updates the session id.

The code in the question checks if there is session passed with the request: first by starting/reactivating the session with session_start(), then checking for an existing cookie matching the session name. If the code finds one, it forces the browser to remove the cookie by setting its expiration date to a time in the past.

PHP Switching between sessions using session_name

Your problem can be solved if you use session_id instead of session_name

// Start our first session with the name 's1'
session_name('s1');
session_start();

$_SESSION['foo'] = 'Foo';
session_write_close();

// Start our second session with the name 's2'
session_name('s2');
//session_start();

$_SESSION['foo'] = 'Bar';
session_write_close();

// We now open our first session and print its value
session_name('s1');
session_start();

print_r($_SESSION['foo']);

Change this equation to

<?php
// Start our first session with the name 's1'

session_id('s1');
session_start();

$_SESSION['foo'] = 'Foo';
session_write_close();

// Start our second session with the name 's2'

session_id('s2');
session_start();

$_SESSION['foo'] = 'Bar';
session_write_close();

// We now open our first session and print its value
session_id('s1');

session_start();
print_r($_SESSION['foo']);
?>

Session_id is an identifier of the session, so it can be different for different session but session_name is just a different name for same session.

PHP: what is the difference between session_name and cookie session name

Yes.The session name references the name of the session, which is used in cookies and URLs .
http://php.net/session_name

PHP Custom Session Name

You'll either need to run session_name() on every file that needs access to the custom session, store the logic in a separate include file which is included within all relevant files, or change PHP's default session name via the session.name configuration value.

Without specifying session_name() in each case, PHP won't know that you want it to use a different name, and instead, default to the value from session.name.



Related Topics



Leave a reply



Submit