PHPAndreas Posted June 26, 2013 Share Posted June 26, 2013 Hello There. My name is Andreas. Well I have begin coding and come to the part at my menu where the username should be in my menu. I have a login form and it look like this: <?php session_start(); $username = $_POST['username']; $password = $_POST['password']; if ($username&&$password) { $connect = mysql_connect("localhost","root","") or die ("Could not connect to MySQL Host"); mysql_select_db("host") or die ("Cound not find MySQL Database"); $query = mysql_query("SELECT * FROM users WHERE username='$username'"); $numrows = mysql_num_rows($query); if($numrows !=0) { while ($row = mysql_fetch_assoc($query)) { $dbusername = $row['username']; $dbpassword = $row['password']; } if ($username==$dbusername&&$password==$dbpassword) { header ("Location: http://localhost/Host/index.php"); $_SESSION['username']=$dbusername; } else echo "Incorrect password"; } else die ("That username does not exists"); } else die ("Please enter a username"); ?> But when I use this php code in my menu: <?php if ($_SESSION['username']) { echo "<li><a href='#'><span>$_SESSION['username'];</span></a> <ul> <li><a href='client/account/logout.php'><span>Logout</span></a></li> <li><a href='client/account/profile.php'><span>Profile</span></a></li> </ul> </li>"; } ?> But i get a error in the session section and I know its something with the $_session code. But I dont know what to type in between the <span> and </span>. This is the image I get in Adobe DreamWeaver: Quote Link to comment Share on other sites More sharing options...
l3rodey Posted June 26, 2013 Share Posted June 26, 2013 Hey Mate, Try this you are missing the " . . " <?php if ($_SESSION['username']) { echo "<li><a href='#'><span>" . $_SESSION['username'] . "</span></a> <ul> <li><a href='client/account/logout.php'><span>Logout</span></a></li> <li><a href='client/account/profile.php'><span>Profile</span></a></li> </ul> </li>"; } ?> or Create a variable like this <?php $username = $_SESSION['username']; if ($username) { echo "<li><a href='#'><span>$username</span></a> <ul> <li><a href='client/account/logout.php'><span>Logout</span></a></li> <li><a href='client/account/profile.php'><span>Profile</span></a></li> </ul> </li>"; } ?> Hope this all helped, Also when echoing use ' instead of " single is faster only a small amount but it is faster in php. so instead of " . . " it would become ' . . ' in singles. Let me know if you have any problems! Brodey Quote Link to comment Share on other sites More sharing options...
PHPAndreas Posted June 26, 2013 Author Share Posted June 26, 2013 (edited) Thanks alot! It worked Edited June 26, 2013 by PHPAndreas Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.