filmov
tv
PHP Session variable creating checking and destroying using session_start() using userid and name
![preview_player](https://i.ytimg.com/vi/eT9-qo4D5Fg/maxresdefault.jpg)
Показать описание
Before sending any data to browser we have to create the session by using session_start().
Sessions are used in PHP in various types of applications to pass data and maintain state of the user. If you are using any membership system where user has to login then after verification we have to keep the login status throughout, so we can maintain the authenticity. To do this we have to use session variables as we will store the userid inside this. Similarly for a shopping cart script we have to store the selected items of the user in session variable and display it to the visitor when required. Sessions are the unique link between the user and the server so all actions of the user can be personalized to the user and handled by the server.
session_start();
$userid="plus2net";
$_SESSION['userid']=$userid; // created session variable
As the member has logged, its user id we can display by showing a welcome message. Here is an example.
echo "Welcome $_SESSION[userid]";
We can check the existence of session with userid at every page level which are supposed to be used by logged in members.
if(isset($_SESSION['userid']) && !empty($_SESSION['userid'])) {
echo " session is available, Welcome $_SESSION[userid] ";
}else{
echo " No Session , Please Login ";
exit;
}
Destroy session
session_unset();
session_destroy();
#SESSION #PHP #CREATE_SESSION
Комментарии