bravo14 0 Posted February 13, 2009 Share Posted February 13, 2009 Hi guys I am trying to create a member only section of a site and display the user name at the top next to the Welcome note. The page is displaying correctly except for the user name <? session_start(); if (!isset($_SESSION['logname'])) { header ("Location: login.php"); } else { echo'<html>'; echo'<head>'; echo'<title>• Yardley Hockey Club • Admin • Control Panel •</title>'; echo'<link href="../style/2008.css" rel="stylesheet" type="text/css" />'; echo'</head>'; echo'<body>'; echo'<div id="maintext">'; include_once('includes/connector.php'); $logname=$_SESSION['logname']; $sql = "SELECT * FROM `tbl_admin_users` where username ='$logname'"; $result=mysql_query($sql); while($row = mysql_fetch_array($result)); echo('Welcome ' .$row[username].'.'); include_once('includes/9.php'); } ?> </div> </body> </html> I know that someone out there can help Link to post Share on other sites
The Little Guy 6 Posted February 13, 2009 Share Posted February 13, 2009 You really should save the user name in a session, otherwise you will rack up tons of queries on your db Link to post Share on other sites
allworknoplay 0 Posted February 13, 2009 Share Posted February 13, 2009 You really should save the user name in a session, otherwise you will rack up tons of queries on your db Exactly, don't use a query in your username everytime the page loads are you crazy!! =) Use the session instead. $_SESSION['username']; Link to post Share on other sites
bravo14 0 Posted February 13, 2009 Author Share Posted February 13, 2009 Cheers guys I am building the page up slowly, once I have got this cracked ,the rest of the page will be determined by the result of the query. Link to post Share on other sites
The Little Guy 6 Posted February 13, 2009 Share Posted February 13, 2009 You really should save the user name in a session, otherwise you will rack up tons of queries on your db Exactly, don't use a query in your username everytime the page loads are you crazy!! =) Use the session instead. $_SESSION['username']; Also, if the user is an administrative user, you should save that in a session too. All of these sessions should be created during login. I personally like to create a session that is set to TRUE if the user is logged in, then I can do this: session_start(); if(!$_SESSION['logged']){ // user is not logged in redirect back home header("Location: /"); exit; } // this is a logged in user do what ever else you want below... Link to post Share on other sites
mryno 0 Posted February 13, 2009 Share Posted February 13, 2009 I'm with these guys. Store the user in the session when you run the log in script. Then there is only one query and it is always available in $_SESSION for other pages. I usually store other info in the session as well like the unique key of the user and whatever other details you may need to identify him in queries down the road. Like The Little Guy said, if he is an admin, throw that in, too. $_SESSION['USER']['is_admin'] = true $_SESSION['USER']['user_id'] = '1234' $_SESSION['USER']['user_first'] = 'John' $_SESSION['USER']['user_nick'] = 'John123' That kind of stuff is always useful. You may want to break it up like I did with all your USER info in one container and so on... If it is just a simple thing that only deals with users, you don't have to. Link to post Share on other sites
Recommended Posts
Archived
This topic is now archived and is closed to further replies.