Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The following appeared when user is NOT logged in but it disappears when a user is logged in:

1. Notice: Undefined variable: uname in C:\xampp\htdocs\fileupload\contact.php on line 9
2. Notice: Undefined variable: uname in C:\xampp\htdocs\fileupload\contact.php on line 30

Here are the codes I am using:
PHP
  1  <?php
  2  	    session_start();
  3  	    if (!isset($_SESSION['username'])) {
  4  	    }
  5  	    else{
  6  		$uname=$_SESSION['username'];
  7  	    }
  8  	    
  9  	    if($uname!=""){
 10  		echo "<a class='brand' href='home.php'>Bais City Division LRMS</a>";?>
 11  		<div class="nav-collapse">
 12  		<ul class="nav">
 13  		    <li><a href="home.php">Home</a></li>
 14  		    <li><a href="about.php">About</a></li>
 15  		    <li class="active"><a href="contact.php">Contact</a></li>              
 16  		</ul>
 17  		<a class="btn btn-primary pull-right" href="logout.php" title="Click to logout"><?=$_SESSION['username']?></a>
 18  	    <?php
 19  	    }
 20  	    else{
 21  		echo "<a class='brand' href='index.php'>Bais City Division LRMS</a>";?>
 22  		<div class="nav-collapse">
 23  		<ul class="nav">
 24  		    <li><a href="index.php">Home</a></li>
 25  		    <li><a href="about.php">About</a></li>
 26  		    <li class="active"><a href="contact.php">Contact</a></li>              
 27  		</ul>
 28  	     <?php
 29  	    }
 30  		if($uname!=""){
 31  		}
 32  		else{
 33  	    ?>
 34  	    <a class="btn btn-primary pull-right" href="filter.php" title="Click to login"></button></a>";
 35  	    }
 36  	    else{
 37  	?>
 38             <a href="index.php?categ=Images"><button class="btn btn-inverse">^__i class=" icon-picture icon-white"__^</i__^Images</button></a>
 39  	   <a href="index.php?categ=Music"><button class="btn btn-success">^__i class=" icon-music icon-white"__^Music</button></a>
 40             <a href="index.php?categ=Videos"><button class="btn btn-primary" id="clickme">^__i class="icon-film icon-white">Videos</button></a>
 41             <a href="index.php?categ=Documents"><button class="btn btn-info">^__i class="icon-file icon-white">Documents</button></a>
 42             <a href="index.php?categ=Text Files"><button class="btn btn-warning">^__i class="icon-file icon-white">Text Files</button></a>
 43             <a href="index.php?categ="><button class="btn">^__i class="icon-check">All Files</button></a>
 44  	<?php } ?>


What I have tried:

I have tried using global tag but the needed buttons which are intended only for logged in users appeared even when no users are logged in.
Posted
Updated 2-Aug-20 6:35am
v3
Comments
[no name] 2-Aug-20 11:29am    
Tried to add line numbers, but unfortunately they don't match your source code. Please add more information what the two problematic lines are. sorry and thanks.
JeffLiteral 2-Aug-20 11:38am    
I have updated already the line numbers where the errors occur. I just hope you could help me with this. Thanks in advance.
[no name] 2-Aug-20 11:49am    
Thanks so far.
Now I have nearly no idea about php, but I'm sure others will help you here.

For me this looks like a key point you should watch:

if (!isset($_SESSION['username'])) {
}
else{
$uname=$_SESSION['username'];
}

and it looks like $_SESSION['username'] is not defined and therefore also $uname remains undefined. But like I mentioned, php is not my field, better wait for the profs ;)

1 solution

Have alook here
PHP
if (!isset($_SESSION['username'])) {
}
else{
   $uname=$_SESSION['username'];
}
PHP
$uname
is not define most probably because
PHP
$_SESSION['username']
is not defined

I hope it helps
 
Share this answer
 
v3
Comments
JeffLiteral 7-Aug-20 4:32am    
Thanks. Works like charm.
[no name] 7-Aug-20 12:14pm    
You are welcome. Thank you very much for accepting and voting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900