$_SESSION[] in php with example
$_SESSION[] is a function of php which helps you to save any text to a variable and allows you to transfer and use that variable in other page.
Here is an example of $_SESSION[] which will help you to understand it's application better.
Before using $_SESSION[], we need to write session_start(), I would suggest to write this on start of your php page so that you don't have to see any error related to this.
Session_start();
$user=$_SESSION["user"];
$_SESSION["user"]='hello';
echo $user
Result- hello
Or in any other file of same folder.
Session_start();
echo $user
Result- hello
note- you can use any text in $_SESSION[]
Like $_SESSION['abc'] or any thing you want to use.
How to use $_SESSION[] in a login page to make it work( php).
Session_start();
$username$_SESSION["username"]
$_SESSION["username"]=$POST['username'];
Now, if you want that only user who are logged in can see a particular page then add this code in that page.
Session_start();
If($username=Null){
echo 'you are not logged in';
}else{
// Write your code //
};
Conclusion:- $_SESSION[] is a very helpful function in php which helps you to save text in variables and use it in other file as we see in the example of login page above.
No comments:
Post a Comment