show logout link in navigation bar when logged in php - php

When user logged in, the login link on nav bar should be gone and logout link should appear how should i do it?
index.html:
<nav>
<p class="menu">Home</p>
<p class="menu">Products </p>
<p class="menu">Login</p>
<p class="menu">Logout</p>
</nav>
Login.php file:
<?php
require 'db.php';
session_start();
$password = $mysqli->escape_string($_POST['Pass']);
$email = $mysqli->escape_string($_POST['EmailAdd']);
$result = $mysqli->query("SELECT * FROM Account WHERE Usermail='$email'");
//check email in db
if ($result->num_rows == 0)
{
$_SESSION['message'] = "Email does not exist";
print '<script type="text/javascript">alert("' . $_SESSION['message'] .
'");
</script>';
header("Location: ../register.html");
}
else
{
//get user array
$user = $result->fetch_assoc();
if ($password == $user['password'])
{
$box = "Login successful";
$_SESSION['email'] = $user['Usermail'];
$_SESSION['logged_in'] = true;
echo ("<SCRIPT LANGUAGE='JavaScript'>
window.alert('Login Successful')
window.location.href='../index.html';
</SCRIPT>");
}
else
{
$_SESSION['message'] = "Wrong password";
header("Location: ../account.html");
echo "failed";
echo '<script language="javascript">';
echo 'alert("Wrong password")';
echo '</script>';
}
}
?>
I've gone through some of the post in stack overflow and apply things like if (!isset($_SESSION['email'])) and else statement on my index.php but its not working and i don't know what's the prob
Ps Previously was using index.php, since its not working so i change it back to index.html

<?php
if(!isset($_SESSION['logged_in'])){?>
<p class="menu">Login</p>
<?php }
else
{?> <p class="menu">Logout</p>
<?php } ?>
try the above code, Hope this helps

Assuming your code is correctly validating the credential and setting the auth state in the session $_SESSION['logged_in'] = true;
You can do something like this:
<nav>
<p class="menu">Home</p>
<p class="menu">Products </p>
<?php if (isset($_SESSION['logged_in']) && $_SESSION['logged_in'] == true): ?>
<p class="menu">Logout</p>
<?php else: ?>
<p class="menu">Login</p>
<?php endif; ?>
</nav>

Try this in your nav:
<?php
if($_SESSION['logged_in'] == "true"){
echo '<p class="menu">Login</p>';
}
else {
echo '<p class="menu">Logout</p>';
}
?>
It is not working because you use the .html extension instead of .php

<nav>
<p class="menu">Home</p>
<p class="menu">Products </p>
<?php if(empty($_SESSION['logged_in'])){ ?>
<p class="menu">Login</p>
<?php } else{ ?>
<p class="menu">Logout</p>
<?php } ?>
</nav>

This should work:
<nav>
<p class="menu">Home</p>
<p class="menu">Products</p>
<p class="menu">
<?php if(isset($_SESSION['logged_in]) && $_SESSION['logged_in]) {?>
Logout
<?php } else { ?>
Login
<?php } ?>
</p>
</nav>

Related

How can I echo it when a user logs in and when a user logs out?

So basically I'm doing my Login system and I came across an error.
<?php
require "header.php";
?>
<main>
<div class="wrapper-main">
<section class="section-default">
<?php
if (isset($_SESSION['userId'])) {
echo '<p class = "login-status">You are logged in!</p>';
}
else {
echo '<p class = "login-status">You are logged out!</p>';
}
?>
</section>
</div>
</main>
<?php
require "footer.php";
?>
$_SESSION['userId'] = $row['idUsers'];
$_SESSION['userUid'] = $row['uidUsers'];
When I press the login button it doesn't echo "You are logged in!". Instead, it keeps echoing "You are logged out!"
<?php
session_start();
require "header.php";
...

PHP - Redirect Loop Error

Everytime I go to my index.php page, I am getting the following error on Google Chrome:
This webpage has a redirect loop
Oh my phpmyadmin database, if the text in account_type is 'deactivate', I made it so the user is automatically redirected to logout.php (This is in the functions.php page)
If it's 'active', then nothing happens.
My index.php code:
<?php include_once 'connect.php';?>
<?php include_once 'functions.php';?>
<!doctype html>
<html lang="en" class="home-page">
<head>
</head>
<body>
<ul class="linklist-admin">
<?php
if (loggedin()){ ?>
<li>My Profile</li>
<?php } else { ?>
<li>Sign In</li>
<?php } ?>
</ul>
</body>
</html>
My functions.php code:
<?php
session_start();
function loggedin(){
if (isset($_SESSION['user_id']) && !empty($_SESSION['user_id'])){
return true;
} else {
return false;
}
}
if(loggedin()){
$my_id = $_SESSION['user_id'];
$user_query = mysql_query("SELECT username, account_type FROM users WHERE id='$my_id'");
$run_user = mysql_fetch_array($user_query);
$username = $run_user['username'];
$account_type = $run_user['account_type'];
$query_level = mysql_query("SELECT name FROM user_level WHERE id='$user_level'");
$run_level = mysql_fetch_array($query_level);
$level_name = $run_level['name'];
$d = 'deactivate';
$a = 'active';
}
if($account_type == $d) { //logs you out if your account is deactivated.
header('location: logout.php');
}
?>
My logout.php code:
<?php
include 'connect.php';
include 'functions.php';
session_destroy();
header('location: index.php');
if($account_type == $d) {
//do stuff here
}
I think that's what you want.

May i know what is wrong with my phpcode?

This is what i have for headers.php anyway it is suppose to be my navigation bar. Here's the problem, when I login with a user as Member the whole header does not come out. But when I login with admin the navigations will "magically" come out!
<?php
if(!isset($_SESSION['sRole'])){
?>
<div id="header">
<div id="fb-root"></div>
<div id ="inthebox">
<b>LOGIN</b>|
<b>REGISTER</b>
</div>
<div id ="outthebox">
HOME|
BOOKSHELF|
SHOPPING CART|
ABOUT|
ABOUT|
</div>
</div>
<?php
}
else{
if($_SESSION['sRole'] == "member"){
?>
<div id="header">
<div id ="inthebox">
<b>LOGOUT</b>
</div>
<div id ="outthebox">
HOME|
BOOKSHELF|
SHOPPING CART|
ABOUT|
PROFILE
<?php
echo("You have Login as :" . $_SESSION['sUsername']);
?>
</div>
</div>
<?php
}else{
if($_SESSION['sRole']=="admin"){
?>
<div id="header">
<div id ="inthebox">
<b>LOGOUT</b>
</div>
<div id="outthebox">
HOME|
BOOKSHELF|
SHOPPING CART|
ABOUT|
Manage Account|
Manage Books|
Manage Orders|
<?php
echo("You have Login as :" . $_SESSION['sUsername']);
?>
</div>
</div>
<?php
}
}
}
?>
This is my doLogin.php page , maybe it might help anyone here to solve this. I store the id, username, firstname and last name into the session. Inside have alr . The website hor when I go in is no error one . no html code error or whatsoever. Just that it does not appear. However the words below the nav links will still come out.
<?php
//connect to database
include ('dbfunction.php');
if (!isset($_POST['Login'])) {
if (isset($_POST['username'])) {
//retrieve form data
$username = $_POST['username'];
$password = $_POST['password'];
$query = "SELECT * FROM users WHERE username='" . $username . "'AND password = '" . $password . "'";
$result = mysql_query($query) or die('The error :' . mysql_error());
$num_rows =mysql_num_rows($result);
if($num_rows == 0){
header('Location:login.php');
exit();
}
//if record is found, store id and username into session
else{
$row = mysql_fetch_array($result);
$_SESSION['sUsername'] = $row['username'];
$_SESSION['sRole'] = $row['role'];
$_SESSION['sFirst_name'] = $row['first_name'];
$_SESSION['sLast_name'] = $row['last_name'];
header('location:successful_login.php');//redirect to this page
exit();
}
}
else {
}
} else {
header('Location:successful_login.php');
exit();
}
mysql_close();
?>
I think the problem here is you are using } else { conditions, but then nesting an if statement inside of them. In fact it looks like you had some broken closing braces. Here is the revised code:
<?php if(!isset($_SESSION['sRole'])){ ?>
<div id="header">
<div id="fb-root"></div>
<div id ="inthebox">
<b>LOGIN</b>|
<b>REGISTER</b>
</div>
<div id ="outthebox">
HOME|
BOOKSHELF|
SHOPPING CART|
ABOUT|
ABOUT|
</div>
</div>
<?php } else if($_SESSION['sRole'] == "member") { ?>
<div id="header">
<div id ="inthebox">
<b>LOGOUT</b>
</div>
<div id ="outthebox">
HOME|
BOOKSHELF|
SHOPPING CART|
ABOUT|
PROFILE
<?php
echo("You have Login as :" . $_SESSION['sUsername']);
?>
</div>
</div>
<?php }else if($_SESSION['sRole']=="admin"){ ?>
<div id="header">
<div id ="inthebox">
<b>LOGOUT</b>
</div>
<div id="outthebox">
HOME|
BOOKSHELF|
SHOPPING CART|
ABOUT|
Manage Account|
Manage Books|
Manage Orders|
<?php
echo("You have Login as :" . $_SESSION['sUsername']);
?>
</div>
</div>
<?php
}
try this:
<?php if( (!isset($_SESSION['sRole'])) || (empty($_SESSION['sRole'])) || (is_null($_SESSION['sRole'])) ): ?>
<html>your code</html>
<?php else: ?>
<?php switch($_SESSION['sRole']) {
case 'admin': { // admin code } break;
case 'member': { // member code } break;
default: { // something happened } break;
}
?>
<?php endif; ?>
check and diagnose the issue

Login/logout dynamic

if(isset($_SESSION['username'])) {
echo "Logout";
} else {
echo "Login";
}
?>
i need someting like this thath i adapted to this bur still wont work...
<?php
if(isset($_SESSION['username'])) {
<p>Ola; <u><?php echo $_SESSION['username']; ?></u>, </p>
<p>Logout</p>;
}
else {
<p>Login</p>;
}
?>
Should i put it in a separed frok html code and include it or? Help me please
I don't know what you want to say and what your actual problem.. but i have checked given url and in my understanding this code may work for you..
<?php if(isset($_SESSION['username'])){ ?>
<p>Ola; <u><?php echo $_SESSION['username']; ?></u>, </p>
<p>Logout</p>
<?php } else{ ?>
<p>Login</p>
<?php } ?>
Thanks...
I don't quite understand your question, but I can see your code has some problems. Try the below.
<?php if(isset($_SESSION['username'])): ?>
<p>Ola; <u><?php echo $_SESSION['username']; ?></u>, </p>
<p>Logout</p>
<?php else: ?>
<p>Login</p>
<?php endif ?>

How to pass variable to isset function in php?

I am trying to return a $user_id variable from file to if(isset(...)) statement. I will perform a different if statement but just trying to echo out the $user_id variable to test that I am setting the page.
<?php
include 'core/init.php';
include 'init.image.php';
protect_page();
include 'includes/overall/overall_header.php';
if(isset($_GET['username']) === true && empty($_GET['username']) === false){
$username = $_GET['username'];
if(user_exists($username) === true){
$user_id = user_id_from_username($username);
$profile_data = user_data($user_id, 'first_name','last_name','email', 'username');
?>
<h1><?php echo $profile_data['first_name']; ?>'s Yor Page</h1>
<div id="navWrapper">
<ul>
<li>
<img src="uploads/profile/blank_profile.gif" width="150" height="150" id="blank_profile">
</li>
<nav>
<ul>
<li>
Albums
</li>
<li>
Music
</li>
</ul>
</nav>
</ul>
</div>
<?php
if(isset($_GET['action']) && $_GET['action']=='albums'){
$albums = get_profile_albums($user_id);
if(empty($albums)){
echo 'No Albums';
}else{
foreach($albums as $album){
if (empty($album['image'])) {
$album['image'] = 'uploads/profile/blank_profile.gif';
}
?>
<p><?php echo $album['name'],' (', $album['count'], ')'?> <br />
<a href="<?php echo $profile_data['username'];?>?action=album_id=<?php echo $album['id'];?>">
<img src="uploads/thumbs/<?php echo $album['id'];?>/<?php echo $album['image'];?>" />
</a><br />
<?php echo $album['description'];?>...<br />
</p>
<?php
}
}
}
if(isset($_GET['action']) && $_GET['action']=='album_id=$album['id']'){
echo $user_id;
}
if(isset($_GET['action']) && $_GET['action']=='music'){
echo'<h1>Music</h1>';
}
}else{
echo 'Sorry, that user doesn\'t exist';
}
}else{
header('Location: index.php');
exit();
}
include 'includes/overall/overall_footer.php';
?>
Single quotes do not parse the string but double quotes do. For example:
$album_id = 1;
echo 'album_id=$album_id ';
echo "album_id=$album_id";
Will result in album_id=$album_id album_id=1
Thus, $_GET['action']=='album_id=$album_id' is checking if $_GET['action'] is equal to 'album_id=$album_id' and not what $album_id is evaluated to.
Your check should be more along these lines:
if (/* ... */ && $_GET['action']=="album_id=$album_id") {
Edit: Suggestion.
You should check $_GET['action'] once and store $action. From there create an if/else if/else statement checking each possible type of action (music, albums, album, etc). If a type of action requires extra user supplied data, include that in the respective if block. For example:
$action = $_GET['action'];
// URL: /script.php?action=albums
if ($action == 'albums') {
// ...
}
// URL: /script.php?action=album&album_id=12
else if ($action == 'album') {
if (!empty($_GET['album_id'])) {
$album_id = $_GET['album_id'];
echo "User with id $user_id is trying to access album with id $album_id";
}
// ...
}
// ...
No more $_GET['action']=="album_id=$album_id"
You are passing the variable correctly.
What I'm more concerned with in the following line is:
if(isset($_GET['action']) && $_GET['action']=='album_id=$album_id'){
'album_id=$album_id' doesnt look right and is probably why your if is not firing... It looks like you're trying to use the variable $album_id wrapped in single quotes and that will not substitute the value and rather keep $album_id in tact. I'm not sure if this is what you intended but if not its likely the source of your problem.

Categories