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.
Related
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>
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
<?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'];
}
I have the below code, which gives no errors, but it only ever displays the Logout button, I don't really have much else to say.
<?php
if(isset($_SESSION)):?>
<div class= "buttons feature">Logout</div>
<?php
else:?>
<div class= "buttons feature">LOGIN</div>
<?php endif;
?>
The Logout button runs:
<?php
session_start();
session_destroy();
?>
And have I have code in my main page which displays values stored in the session based on if the session isset, so I know it is destroying the session, and when I'm logged in it is set.
I just don't understand why it always only displays the html from the if where the session doesn't exist.
I've probably made a silly mistake, so sorry if I have.
All help appreciated -Tom
try this
if(isset($_SESSION) AND $_SESSION):?>
<div class= "buttons feature">Logout</div>
<?php
else:?>
<div class= "buttons feature">LOGIN</div>
<?php endif; ?>
Setting $_SESSION
session_start();
$_SESSION['logged_in'] = true;
checking $_SESSION:
session_start();
if(isset($_SESSION['logged_in'])){
echo "logged in";
}
The $_SESSION variable is always set - regardless if you have initialized the session or not. You must actually assing something in $_SESSION array to get results. For example after succesful login do this:
$_SESSION['is_logged_in'] = true;
And then you can check
if(isset($_SESSION['is_logged_in']))
anywhere you want (instead of your if(isset($_SESSION))).
Please can someone help me with this. I have this MySQL query which lists users on my site, and echos a link that can be clicked to take you to the user's profile:
<?php
$online_set = online_channel();
while ($online = mysql_fetch_array($online_set)) {
echo "<div class=\"online_row\"><img width=25px height=25px src=\"data/photos/{$online['user_id']}/_default.jpg\" class=\"online_image\"/><div class=\"online_text\">{$online['display_name']}</div></div>";?>
<? } ?>
The link goes to profile.php and echos the users 'user_id' so it knows which users profile to take you to, and now I am wanting to include a session variable in the link somehow so a message is displayed on that users profile after clicking the link.
I have tried including $_SESSION['chat'] in the link but it doesn't work:
<?php
$online_set = online_channel();
while ($online = mysql_fetch_array($online_set)) {
echo "<div class=\"online_row\"><img width=25px height=25px src=\"data/photos/{$online['user_id']}/_default.jpg\" class=\"online_image\"/><div class=\"online_text\">{$online['display_name']}</div></div>";?>
<? } ?>
I am also trying to execute the session in profile.php by using this:
<?
session_start();
if(isset($_SESSION['chat']))
echo $_SESSION['chat']="<div class=\"infobox-favourites\"><strong>Deleted from Favourites</strong> - This user has successfully been deleted from your favourites.</div><div class=\"infobox-close4\"></div>";
unset($_SESSION['chat']);
?>
What I have tried is not working and i'm not sure i'm doing it right, so I would really appreciate any help with this. Thanks
Try this...for easier debugging the entire code you have
<?php
$online_set = online_channel();
while ($online = mysql_fetch_array($online_set)) {
?>
<div class="online_row">
<a href="profile.php?id=<?php echo $online['user_id'];?>">
<img width=25px height=25px src="data/photos/<?php echo $online['user_id'];?>/_default.jpg/" class="online_image" />
<div class="online_text\">
<?php echo $online['display_name'];?>
</div>
</a>
</div>
<?php
}
?>
Just review the code I posted to learn the better way to echo an entire div