php one page 2 result - php

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 ===

Related

The cause of the PHP error in the role section [duplicate]

First page
<?php
session_start(); // put ahead all html tags and echo commands and print.
$_SESSION["username"] = 'admin';
echo 'see session';
?>
Second page
<?php
if( $_SESSION["username"] == 'admin' ) {
echo 'Hello '. $_SESSION["username"] . ' You are adminstrator on this page';
} else {
echo 'You can not accesss';
}
?>
Question
When I click on the link session then I get :
Undefined variable: _SESSION
I have no idea why.
Add in your second file at the start session_start();
like that:
<?php
session_start();
if( $_SESSION["username"] == 'admin' )
{
echo 'Hello '. $_SESSION["username"] . ' You are adminstrator on this page';
}
else
{
echo 'You can not accesss';
}
?>
You need to put session_start(); at the begin of the second page.

Is it possible to modify PHP's $_SESSION variable by client

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 !";
}

PHP session variable cannot be read in other page

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?

echo a message on a new page

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

converting session statement?

i've just started learning php, i'm using an if statement to control what will be displayed on my webpage depending on whether a user is logged in or not.
I know how to show things to the user if they are logged in under their session, by using this session statement. But how would i create the reverse to tell the site to display something else if the user is not logged in under their session?
Here's the code i'm using.
<?php if (isset($_SESSION['user_id'])) {
if ($user['id'] == $_SESSION['user_id']){
if ($user['account_type'] == "platinum"){ ?>
<html stuff>
<?}
}
}?>
It sounds like you are new to programming, not just PHP. Welcome to the community!
You are looking for the else part of the if-statement. This is common to many programming languages.
Here is an example to get you started.
<?php
if (isset($_SESSION['user_id']))
{
if ($user['id'] == $_SESSION['user_id'])
{
if ($user['account_type'] == "platinum")
{
print "Welcome, platinum user";
}
else
{
print "Welcome, non-platinum user";
}
}
else
{
print "Unexpected session";
}
}
else
{
print "You are not logged in";
}
?>

Categories