Need help to show different text when logged in

Im trying to make a way to display one text if logged in and another if not logged in. i am having problems is with when im calling for the session. i have no experience with php and have gotten alittle help from many people.

HTML:

[code]<?php if(empty($login_session)){?>
Register
Login

<?php } else { ?>
Signed in as :  <?php echo $login_session; ?>Logout
<?php } ?>[/code]

This is the code im working with to call for the $login_session:
[php]<?php
include(‘config.php’);
session_start();
$user_check=$_SESSION[‘login_user’];

$ses_sql=mysqli_query($obj->conn,"SELECT username FROM users WHERE username=’$user_check’ ");

$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

$login_session=$row[‘username’];

if(!isset($login_session))
{
$ses_sql=mysqli_query($obj->conn,"SELECT username FROM users WHERE username=’$user_check’ ");

$row=mysqli_fetch_array($ses_sql,MYSQLI_ASSOC);

?>[/php]

Well, your HTML uses PHP variables to display if they are set or not. If not set, it shows the register or
login options. If set, it shows the user as $login_session. One problem with this is you are using the
empty() function to check for the value in $login_session, you should make sure you UNSET it before you
set it. In your second part, you have code that sets the $login_session variable no matter what. So, it
is never “empty”…

Normally, you would run your query to select the username form the users table, but, you would check to
see if it is there. I usually check the mysqli_num_rows() of the results to make sure that only one user is
found. ( If more, then, you have a database error, if less, no user found. ) If the user is found, then you
can set the value of the $login_session. So, you need to add " UNSET ($login_session); " before you do
the query. Then, your HTML code will show the correct option.

Also, PHP remembers variables until you close the session or UNSET the variable. So, you can run into some
issues if you do not make sure you are pulling out valid data after running queries.

Lastly, you are running duplicate queries. You do NOT need the second query. Just SELECT * FROM users,
then you have all the data. Check the number of rows you have, if you got zero, throw an error, if you get
a row, then you already have the rest of the data… Hope that makes sense…

Sponsor our Newsletter | Privacy Policy | Terms of Service