princeofpersia Posted January 6, 2011 Share Posted January 6, 2011 Hi guys, what im trying to do in code below is to avoide public visitors (not registered) to see then content if they are not logged in, but when i visit the site as a public visitor this is the only thing i get Welcome, ! in my code below im trying to see if user exist, if not it should show the echo"You need to login as a user to view this page"; can u put me in right direction on how to do this please? thanks in advance <?php session_start(); include ("global.php"); //username session $_SESSION['username']=='$username'; $username=$_SESSION['username']; $auth=mysql_query("SELECT username FROM users WHERE username='$username'"); while($row = mysql_fetch_array($auth)) { $exist_user=$row['username']; } if ($username!=$exist_user) { echo"You need to login as a user to view this page"; } else { the rest of the code goes here } Link to comment https://forums.phpfreaks.com/topic/223560-is-this-right/ Share on other sites More sharing options...
MMDE Posted January 6, 2011 Share Posted January 6, 2011 I wouldn't say too much is right here... //username session $_SESSION['username']=='$username'; $username=$_SESSION['username']; $auth=mysql_query("SELECT username FROM users WHERE username='$username'"); == is not assigning, but comparing... and there's no "if". I doubt you are trying to compare though... why assign username to username session, then set the username variable from the session, then search in the mysql db after just the username when the username is already known. I think you need to provide more of your code. $query="SELECT username FROM users WHERE username='$username'" $result=mysql_query($query) or die(mysql_error()); if(mysql_num_rows($result)==1){ $row=mysql_fetch_assoc($result); echo 'Welcome'.$row['username']; }else{ echo 'you need to log in'; } Link to comment https://forums.phpfreaks.com/topic/223560-is-this-right/#findComment-1155625 Share on other sites More sharing options...
Anti-Moronic Posted January 6, 2011 Share Posted January 6, 2011 Not sure if this reflects the original source but you should definitely indent your code Link to comment https://forums.phpfreaks.com/topic/223560-is-this-right/#findComment-1155629 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.