I use PHP in my website project work. I have put session value after login success, but I want to check value is exist or not in the session variable to the redirect page. How can I check it?
Give me example code. I have set session variable with this code:
<?php if($login==true){ $_SESSION["user_id"]="some value"; //Then set redirect page ?>
If session value is exist then I want to display existing session value.
First you need to put some value in the session variable as like $_SESSION[“session_variable”]=”session value” then you can check it. If you already done it, so that’s good.
Here you have submitted form data into database and if submit is success then you set a value in the session variable and set redirect page. So now you can easily check value exist in session variable or not.
You should be careful, where you use session so must be start session like session_start();. I give you a example or source code for check session value is exist or not, so follow the example.
Put value in a session variable after success work:
<?php session_start(); if($sql==true){ $_SESSION["user_id"]=$_POST["user_id"]; //Next Redirect Page } ?>
Now check value is exist or not:
<?php session_start(); if($_SESSION["user_id"]==""){ echo "No value in this session variable"; }else{ echo "Value exist"; } ?>
If you use session_start() in a page then you do not use session_start() twice. You try to put session_start() in the Database connection page or set below of db connect code. Hope it will be helpful for you.