The $_SESSION['isLogin'] default value is 0.
I want to modify the $_SESSION['isLogin'] value to 1
(not in the script)
<?php
session_start();
$_SESSION['isLogin'] == 1 {
echo "Hello";
} else {
echo "You need login before enter this page !";
}
?>
Yes you can modify the value of $_SESSION variable.You can try for
session_start();
$_SESSION['isLogin'] = 1;
if(isset($_SESSION['isLogin'])) {
echo "Hello";
} else {
echo "You need login before enter this page !";
}
Related
I am building a login page using php.
I don't know why the session variable does not work.
Here is my login.php
<?php
function loginFail()
{
echo "<script> alert(\"Invalid user name or password\");";
echo "location.href=\"index.php\";";
echo "</script>";
}
if (empty($_POST["loginName"])|| empty($_POST["adminPwd"])) {
loginFail();
} else {
$userName=$_POST["loginName"];
$password=$_POST["adminPwd"];
if (($userName=="user") && ($password=="password")) {
session_start();
$_SESSION["isLogined"]="Ok";
header("location:admin.php");
} else {
loginFail();
}
}
?>
Here is admin.php
<?php
include 'checkSession.php';
?>
This is checkSession.php source code
<?php
function loginFail()
{
echo "<script> alert(\"Session Expired, please login again\");";
echo "location.href=\"index.php\";";
echo "</script>";
}
echo $_SESSION["isLogined"];
?>
The output of checkSession.php is :
Undefined variable: _SESSION
I have tried replace
header("location:admin.php");
with
echo "<script>";
echo "location.href=\"admin.php\";";
echo "</script>";
However, it still does not work.
Furthermore, I have tried replace :
header("location:admin.php");
With
echo $_SESSION["isLogined"];
It can display the word "Ok".
Would you tell me how to fix the problem?
Using PHP I am trying to check whether the user is an admin or a normal user. If they are a normal user then it should redirect them to home.php. However if they are an admin then it should redirect them to admin.php. When entering in the username and password of a standard user it sends me to home.php which is what is expected. However whenever I use an admin login it does not direct me to admin.php. Instead it redirects me to authentication.php which is the script that checks if the username and password are correct.
I have tried 2 different blocks of code, the first one did not work at all as it came up with syntax errors. However this one should work fine but it does not. I have also checked if the correct numbers are being stored within the database. 1 being a standard user and 2 being for an admin. I have printed the admin level on my profile page so I know that it is storing the data properly.
if (isset($_SESSION['admin'])) {
if ($_SESSION['admin'] === "2") {
header('Location: admin.php');
} else {
header('Location: home.php');
}
} else {
header('Location: home.php');
}
I expect that when an admin logs in it should direct me to admin.php, however I am just being directed to a blank screen of authentication.php.
Your code is accurate. Forexample
<?php
$_SESSION['admin'] = "2";
if (isset($_SESSION['admin'])) {
if ($_SESSION['admin'] === "2") {
header('Location: admin.php');
} else {
header('Location: home.php');
}
} else {
header('Location: home.php');
}
?>
If you run this on localhost it redirects to admin.php page properly. as you are using === so you have to make sure your datatype is matched.
for example:
<?php
$a = "1";
if ($a === 1) {
echo "not ok";
}
if ($a == 1) {
echo "ok";
}
if ($a === "1") {
echo "This time ok";
}
if ($a == "1") {
echo "it's also ok";
}
?>
First echo doesnt show as if cant matched datatype. so i suggest you to change === to == and see. hopefully it will solve your problem
<?php
if (isset($_SESSION['admin'])) {
if ($_SESSION['admin'] == "2") {
header('Location: admin.php');
exit();
} else {
header('Location: home.php');
exit();
}
} else {
header('Location: home.php');
exit();
}
?>
You can return the link page from the php script like below:
if (isset($_SESSION['admin'])) {
$data['admin_page_link'] = 'admin.php';
echo json_encode($data);
}else {
$data['home_page_link'] = 'home.php';
echo json_encode($data);
}
Then just call it in javascript ajax call to redirect to the desired page:
if (response.user === 'admin') {
window.location.href = response.admin_page_link;
} else {
window.location.href = response.home_page_link;;
}
In the above code you may check by using the full web address of php file like: http://localhost/admin.php
echo $_SESSION['area'];
output:Hello;
if($_SESSION['area'] == 'Hello'){
echo 'Working Fine';
}else{
echo "Not Working";
}
output:Not Working
I trying to find the reason for this strange result.
Try this code you got that result because session may not have set
<?php
// Start the session
session_start();
// Set session variables
$_SESSION["area"] = "Hello";
if($_SESSION['area'] == 'Hello'){
echo 'Working Fine';
}else{
echo "Not Working";
}
?>
store your session into variable like this
$area=$_SESSION['area'];
and use it like
if($area == 'Hello'){
echo 'Working Fine';
}else{
echo "Not Working";
}
this is my code after success enter login and password
<?php session_start();
if(!isset($_SESSION['nik'])){ die("Anda belum login");}
if($_SESSION['level']!="admin"){
echo "<h3>Welcome ".$_SESSION['nik']."</h3>";
echo "panel user";
}
if($_SESSION['level']="admin"){
echo "panel admin";
}
?>
<a href=log.php?op=selesai>Log Out</a>
the out put that i want is :
if login = admin then display "hello admin"
if login = user then display "hello user"
if not login then say "you must login"
any suggestion how to fix that ?
please find below code as per your requirements.
<?php
session_start();
if(!isset($_SESSION['nik'])){
echo "You must login.";
exit;
}
if($_SESSION['level']!="admin"){
echo "<h3>Welcome ".$_SESSION['level']."</h3>";
echo '<a href=log.php?op=selesai>Log Out</a>';
exit;
}
else {
echo "<h3>Welcome ".$_SESSION['level']."</h3>";
echo '<a href=log.php?op=selesai>Log Out</a>';
}
?>
Thanks.
i choose and use #Ghanshyam answer,
thank you to all member at stackoverflow.
this is very helpfull site
Use following statement to set session for username while login-
session_start();
//following statement will create a session store user name.You can use this variable to display particular output.
$_SESSION["uname"] = $_GET['un'];
//in above statement 'un' is text box from which we are accepting username.
After set session variable for user name you can use it on another page-
session_start();
if(isset($_SESSION["uname"]) )
{
if($_SESSION["uname"] == 'admin')
{
echo 'hello admin';
}
else($_SESSION["uname"] == 'uname')
{
echo 'hello user';
}
}
else
{
echo "you must login"
}
The problem is in your operator selection.
if($_SESSION['level']="admin"){
echo "panel admin";
}
should be
if($_SESSION['level'] == "admin"){
echo "panel admin";
}
= is an assignment operator. And you need to use == or ===
I want to echo a message on a new page after redirecting. but I only want the message to show once after reloading (redirecting) and on the next reload I want the message gone. is this at all possible? I give you an example:
$_SESSION['message'] = "entry deleted"
header("location: anotherpage.html")
on "anotherpage.php"
echo "$_SESSION['message']" // upon next reload $_SESSION['message'] = "";
echo $_SESSION['message'];
unset($_SESSION['message']);
But I prefer to add a function to display messages, something like:
function setMessage($msg_body, $css_style = 'normalMsg') {
$_SESSION['messages'][$css_style][] = $msg_body;
}
function showMessages() {
if (empty($_SESSION['messages'])) return;
foreach ($_SESSION['messages'] as $css_style=>$messages) {
echo '<div class="'.$css_style.'">';
echo implode($messages,'<br>');
echo '</div>';
}
unset($_SESSION['messages']);
}
You can check, if message is set and valid
if (isset($_SESSION['message']) && $_SESSION['message']) {
echo $_SESSION['message'];
$_SESSION['message'] = false;
}
It should be working as you suggested. (If I got you right).
Simply:
the redirecting page :
$_SESSION['message'] = 'test';
header('Location: anotherpage.php');
on "anotherpage.php" :
echo $_SESSION['message'];
$_SESSION['message'] = ''; // message will being empty on further reloads