Definition and Usage of PHP Session_Name
Sessions, also called “session handling,” are a way for a web app to make data available on different pages.
With the session_name()
function, you can give the current session a name or find out what that name is.
Additionally, if a new session name
is supplied, session_name() modifies the HTTP cookie (and output content when session.transid
is enabled).
Once the HTTP cookie is sent, session_name() raises error. session_name() must be called before session_start() for the session to work properly.
The session name is reset to the default value stored in session.name
at the request startup time.
Thus, you need to call session_name() for every request (and before session_start() is called).
Syntax:
session_name( [ name ] );
Parameters:
name – If you want to set the name of the session with this method, this is a string value that shows the name of the session.
Return Values:
If you don’t give this function any parameters, it will return a string with the name of the current session.
If you passed a string value to set the name of the current session, this function returns the name of the old session.
Changelog
Version | Description |
---|---|
8.0.0 | name is nullable now. |
7.2.0 | session_name() now checks the status of the session. Before, it only checked the status of the cookie. So, older versions of session_name() let you call session_name() after session_start() , which can cause PHP to crash or act strangely. |
Example of PHP Session_Name
Example 1:
The session_name()
function is shown in action in the next example.
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php
//Starting the session
session_start();
$name = session_name();
print("Session Name: ".$name);
?>
</body>
</html>
When the above html file is executed, the following message is displayed:
Session Name: PHPSESSID
Example 2:
Here’s another example of this function in action. In this case, we have two pages from the same application open at the same time.
session_page1.php
<html>
<body>
<form action="#" method="post">
<label for="fname">Enter the values click Submit and click on Next</label>
<br><br><label for="fname">Name:</label>
<input type="text" id="name" name="name"><br><br>
<label for="lname">Age:</label>
<input type="text" id="age" name="age"><br><br>
<input type="submit" name="SubmitButton" />
<?php
if(isset($_POST['SubmitButton'])){
//Setting the session name
session_name("my_session");
//Starting the session
session_start();
$_SESSION['name'] = $_POST['name'];
$_SESSION['age'] = $_POST['age'];
print("<br><br>Session name: ".session_name());
}
echo '<br><br><a href="session_page2.php">Next</a>';
?>
</form>
</body>
</html>
This will result in the following output:
Once you press submit the page will be like this:
When you click on Next, the next file is run.
session_page2.php
<html>
<head>
<title>Second Page</title>
</head>
<body>
<?php
//Changing the session name again
$s_name = session_name();
//Starting a Session
session_start();
print($_SESSION['name']);
echo "<br><br>";
print($_SESSION['age']);
echo "<br><br>";
print("Previous Session Name: ".$s_name);
?>
</body>
</html>
This will result in the following output:
Grace
25
Previous Session Name: PHPSESSID
Example 3:
Using this function, as shown below, you can give your session a name that you choose.
<html>
<head>
<title>Setting up a PHP session</title>
</head>
<body>
<?php
//Starting the session
session_start();
$id = session_create_id("test-");
print("Id: ".$id);
?>
</body>
</html>
When the above HTML file is run, the following message will be shown.
Id: test-es6j8171lluesjrokpvqqb4b4d
Frequently Asked Questions (FAQs)
What is the PHP session name?
The session name is used in cookies and URLs to refer to the name of the session (e.g. PHPSESSID ).
It should only have letters and numbers, and it should be short and clear (i.e. for users with enabled cookie warnings).
How do you give a session a name?
session_name()
tells you what the current session’s name is.
If name is given, session_name()
will change the name of the session and return the old name.
What is the default PHP session name?
session_name()
tells the cookie name what the name of the session is.
It should only have letters and numbers. PHPSESSID is the default.
How do I find my session name?
session_name()
tells you what the current session’s name is.
If name is given, session_name()
will change the name of the session and return the old name. session_name()
changes the HTTP cookie if a new session name is given (and output content when session. transid is enabled).
Related Articles
- PHP includes With Detailed Explanation
- Creating a file in PHP With Examples
- PHP Remove Element From Array With Examples
- How to Upload file using PHP with Advanced Example
- PHP Ucwords (With Advanced Examples)
Summary
In summary, we have learned how to create and execute the session_name
in PHP.
By learning all this, we can apply this topic to various programming applications.
Lastly, if you want to learn more about session_name
in PHP, please leave a comment below. We’ll be happy to hear it!