Change text when logged in - php

I want to change my navigationbar from saying:
Register
Login
To:
<div id="nav-login">Signed in as : <strong><?php echo $login_session; ?></strong><a id="nav-register" href="logout.php">Logout</a></div>
Where <?php echo $login_session; ?> is used to get the loged in users username.
I have not much experience with PHP so please explain it easy.

Try this method
<?php if(isset($login_session) && !empty($login_session)){ ?>
<div id="nav-login">Signed in as : <strong><?php echo $login_session; ?>
</strong><a id="nav-register" href="logout.php">Logout</a></div>
<?php }else{ ?>
Register
Login
<?php } ?>
Make sure $logged_session is defined and if you using $_SESSION variable use session_start(); before creating/using $_SESSION variable

Related

Problem with PHP Sessions to get it to echo to my html page

I am trying to figure out how to bring use session_start() to get the $name variable stored in my Mysql to echo it to my page. I have tried many different ways including $_GET['name'] but it is showing different kinds of error. Database, signup and login are working perfectly. Values are stored in Mysqli database. Any solution or help? Here is my code:
//php:
<?php
session_start();
if(!isset($_SESSION['name']) || empty($_SESSION['name'])){
header('Location: /');
exit();
}
$name = $_SESSION['name'];;
?>
//html:
<div class="response">
<h2>Thank you, <?php echo $name; ?> for joining the waitlist!
</h2>
<br>
<p>Here is your referral link:
https://mylink/waitlistRegister.php?refer=<?php echo $name; ?>
</p>
</div>
I see that you are not defining your session properly.
Try:
<?php
session_start();
if(!isset($_SESSION['name']) || empty($_SESSION['name'])){
header('Location: /');
exit();
}
$name = "Your Name";
$_SESSION["name"] = $name;
?>
<div class="response">
<h2>Thank you, <?php echo $_SESSION["$name"];; ?> for joining the waitlist!
</h2>
<br>
<p>Here is your referral link:
https://mylink/waitlistRegister.php?refer=<?php echo $_SESSION["$name"]; ?>
</p>
</div>

php log in with session display

Hi I am trying to make a website prototype that will show log in username , i have successfully displayed the user session, but when there is no session it will only display error.
<h1>Dear Precious Customer</h1>
<div>
<h4> Welcome <?php echo $_SESSION['username']; ?> </h4>
</div>
<div>
Logout
</div>
Here is my code for the display, as you can see it only show when session is active , if there is no session it will only show error . Instead of that error, how do I make it display something like "welcome log in or register" thanks.
Change this
<div><h4>Welcome <?php echo $_SESSION['username']; ?></h4></div>
To
<?php
$msg = 'Welcome! Login | Register';
if ($_SESSION && isset($_SESSION['username'])) {
$msg = 'Welcome! ' .$_SESSION['username'];
}
?>
<div><h4><?php echo $msg;?></h4></div>
You would need to test for the session variable like,
<div><h4>Welcome <?php echo (isset($_SESSION['username']) ? $_SESSION['username'] : 'Register'); ?></h4></div>
That way if it's not set, you simply output the other desired string.

How to restrict the view for particular user when they are login in using cookie name using php

This is my php code
<?php
require_once('../includes/config.php');
// include file to check, If current user loggedin or not
include('log-security.php');
require_once('../cs/includes/header.php');
if(in_array(cms_username, $allowedUsers))
{
$allowedUsers=['john', 'akhil'];
<div id="view8"></div>
}
?>
This is my div id code
<div id="view8" class="tabcontents">
<h3>Creation of files</h3>
</div>
Im new to PHP, Please help me to correct the code if its wrong .
while login only it should restrict the user.when allowed user is entered then the viewid should displayed for others it should not.
Try something like that :
<?php
require_once('../includes/config.php');
require_once('../cs/includes/header.php');
include('log-security.php');
$allowedUsers = array('john', 'akhil');
?>
<?php if (in_array('cms_username', $allowedUsers)): ?>
<div id="view8" class="tabcontents">
<h3>Creation of files</h3>
</div>
<?php endif; ?>

How to pass session variables when header redirects? loosing session variables during the process of redirection

session varibales
<?php session_start();
error_reporting(-1);
if (!isset($_SESSION)){
}
$total_amt=$_POST['total_amt'];
$total_seats=$_POST['total_seats'];
$seat=$_POST['seat'];
$boarding_point=$_POST['boarding_point'];
$submit=$_POST['chksbmt'];
$_SESSION['total_amt']=$total_amt;
$_SESSION['total_seats']=$total_seats;
$_SESSION['seat']=$seat;
$_SESSION['boarding_point']=$boarding_point;
$_SESSION['chksbmt']=$submit;
?>
<?php
$serial_no = 1;
$total_seats = $_SESSION['total_seats'];
$seats=explode(',', rtrim($total_seats, ","));
foreach ($seats as $seat) { ?>
<tr>
<td style="text-align:center;"> <?php echo $serial_no++; ?></td>
<td style="text-align:center;"><?php echo $seat; ?> </td>
& so on ..
login div
<ul class="top">
<li>Registration
</li>
<?php if(!isset($_SESSION['login_user'])) { ?>
<li class="hover">Login
</li>
<?php } else {?>
<li class="hover">Logout</li>
<div style="color:#00CC66;">Welcome <?php echo $_SESSION['login_user']; ?></div>
<?php } ?>
</ul>
logout.php
<?php session_start();
session_destroy();
header('Location:'.$_SERVER['HTTP_REFERER']);
exit;
?>
When m logging out from my page, it has to be redirected & in this process i m loosing my session variables. Is that any process to keep the variables constant after the redirection .. After the page redirection m getting undefined index error.. Any help .. ?? Thank you in advance
You're destroying whole session on logout page. While you just need to unset the relevant session key.
Something like,
session_start();
unset($_SESSION['login_user']); //session_destroy();
header('Location:'.$_SERVER['HTTP_REFERER']);
exit;
instead of using session_destroy(); you should use unset($_SESSION['login_user']);It will change the value of the variable in the session to null and you will not lose your variable. session_destroy() destroys the session completely and then you have no way to access that variable.
You can check the difference between them using var_dump eg:- session_destroy(); var_dump($_SESSION['login_user']); and unset($_SESSION['login_user']); var_dump($_SESSION['login_user']);
AND in your code
if (!isset($_SESSION)){
}
you should not keep it blank. I think you forgot to include the following code inside this block

Creating a simple "Logged In As" line on my page

<?php
session_start();
if(isset($_SESSION['login']))
{
include_once('includes/header.php'); ?>
<!DOCTYPE html>
<html>
<body>
<div id="mainframe">
<img src="img/header.png">
<div id="menu">
<?php include_once('includes/navbar.php'); ?>
</div>
<div id="content">
<h3>Shopping Cart</h3>
</div>
</div>
<?php include_once('includes/footer.php'); ?>
</body>
</html>
<?php }
else
{
header('location: login.php');
}
?>
Here is my small PhP code I've got at the moment, my login session is $_SESSION['login'].
And I'd like to display : Logged in As on my page when they are logged in, I've tried several things but it didn't work out.
Does anyone know a simple method / solution for this?
Put this somewhere in your if statement.
It will show Logged in as User at right top corner of page
<div style="position:absolute; right:0px; top:0px;">
<?php echo "Logged In as". $_SESSION['login']; ?>
</div>
U need to pass username using SESSION variable for the same
write a simple sql query to get the username from any variable you are taking from user to make sure that the particular user is the correct user.i am taking password.
$query = "SELECT name FROM users WHERE password='$password'";
$username = mysql_result(mysql_query($query),0);
$_SESSION['username'] = $username;
than proceed as you are doing
<?php
session_start();
if(isset($_SESSION['login']) && isset($_SESSION['username']))
{
echo "logged in as".$_SESSION['username'];
}

Categories