zang8027 Posted January 27, 2009 Share Posted January 27, 2009 Ok, i have a table that has the following columns: User Name | User Password | User Type (enum) Now, i have a few pages on the site that are only available if you are signed in. These pages have the following php at the top: <?php //Refuse letting the user in unless logged in include("security.php"); session_start(); ?> Here is security php <?php session_start(); //if they bypassed login and are not logged in if(!isset($_SESSION['priv'])){ //redirect header("Location:login.php?error=bypass"); } ?> Everything works fine there. The question that I have is that is there a way to show what user name is logged in? I want a to pull rows from another table where userID is equal to whoever is logged in. IS there extra code that I need in my log in? Here is the process login page that logs the user in. <?php//Store variables the users choices from index page: $namechoice=$_GET['userName']; $passchoice=$_GET['userPass']; //Make a connection to the server... connect("servername","username","password") (or die is optional to show error) $link=mysql_connect("localhost","clikndin_tyler","$dbPass") or die("No server connection".mysql_error()); //connect to our database $db=mysql_select_db("clikndin_clickndine") or die("No Database Connection ".mysql_error()); //construct a SQL query that selects users from DB $query="SELECT * FROM clientTable WHERE userName='$namechoice' AND userPass='$passchoice'"; //run the query $result=mysql_query($query); //if there was a row returned.... if($row = mysql_fetch_array($result)) { //begin a session, which lets us store sessions vars session_start(); //store users infor a var to be used later on ($_SESSION[NEWNAME, ANYTHING]=$row['COLUMNNAME'] $_SESSION['priv']=$row['userType']; //send user to main page: header("Location:loginConfirm.php"); } else { //close the connection mysql_close($link); //redirect the browser back to index with string that activates message header("Location:login.php?error=nouser"); } ?> Quote Link to comment Share on other sites More sharing options...
trq Posted January 27, 2009 Share Posted January 27, 2009 Store the users username in the $_SESSION array. eg; $_SESSION['username'] = $row['userName']; Quote Link to comment Share on other sites More sharing options...
zang8027 Posted January 27, 2009 Author Share Posted January 27, 2009 then what would i use for security.php and such Quote Link to comment Share on other sites More sharing options...
bluesoul Posted January 27, 2009 Share Posted January 27, 2009 You wouldn't have to modify any code, it's simply a matter of convenience to have the username stored as a variable. It's not overwriting your 'priv' element. Quote Link to comment Share on other sites More sharing options...
zang8027 Posted January 27, 2009 Author Share Posted January 27, 2009 ok cool thank you. Now, how do i get the username from the session tho. That is what i am really not sure on. say i want to print out userName of the session, what would i type? Echo "... "; Quote Link to comment Share on other sites More sharing options...
trq Posted January 27, 2009 Share Posted January 27, 2009 echo $_SESSION['username']; 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.