stublackett Posted February 25, 2008 Share Posted February 25, 2008 Hi, I'm wanting to load the "UserID" variable from my Database I've managed to select userlevel, No problems - Did this using sessions, But it just wont output userID in my echo statement Any ideas at all ?? loginsuccess.php <?php session_start(); include ("dbconnect.php"); if(!isset($_SESSION['username'])){ $message = <<<HTML This page is for Registered users only <br /> <br /> Register to the site, <br /> <br /> By following the link in the menu on the left HTML; $userID = $_SESSION['UserID']; $page = <<<LOGINFORM <form method="post" action="" login=true"> <fieldset> <div> <label for="username">User Name :</label> <input type="text" name="username" id="username" class="txt" /> </div> <div> <label for="fullname">Password : </label> <input type="password" name="password" id="password" class="txt" /> </div> </fieldset> <div> <input type="submit" name="Submit" id="Submit" value="Login" class="btn" /> </div> </fieldset> </form> LOGINFORM; }else{ //Protected Area sits below $message = "Welcome to the Dragons Den ".$_SESSION['username']; } $userlevel = $_SESSION['userlevel']; //If Userlevel is 1 show the Entrepeneurs' Form (On the left) if($userlevel == "1"){ //Entrepeneur List $page = <<<ENTREPENEUR <p><b>Welcome to the site $username</b> </p> <ul> <li><a title="Add an Idea" href="ideas.php">Add An Idea</a> <br /></li> <li><a title="View Your Ideas" href="ideas.php">View Your Ideas</a> <br /> </li> <li><a title="View Your Offers" href="offers.php">View Your Offers</a></li> </ul> ENTREPENEUR; } //Otherwise show the Dragons' Form (On the left) else if($userlevel == "2"){ //Dragon List $page = <<<DRAGON <p><b>Welcome to the site $username</b> </p> <ul> <li>View Latest Ideas </li> <li>Make an Offer</li> <li>Review Offers </li> <li>View All Ideas</li> <li>Manage Your Profile</li> </ul> DRAGON; } ?> Login <?php session_start(); include ("dbconnect.php"); //Show LoginForm using HEREDOC $page = <<<LOGINFORM <form method="post" action="" login=true"> <fieldset> <div> <label for="username">User Name :</label> <input type="text" name="username" id="username" class="txt" /> </div> <div> <label for="fullname">Password : </label> <input type="password" name="password" id="password" class="txt" /> </div> </fieldset> <div> <input type="submit" name="Submit" id="Submit" value="Login" class="btn" /> </div> </fieldset> </form> LOGINFORM; $username = $_SESSION['username']; $userlevel = $_SESSION['userlevel']; //If Userlevel is 1 show the Entrepeneurs' Form (On the left) if($userlevel == "1"){ //Entrepeneur List $page = <<<ENTREPENEUR <p><b>Welcome to the site $username</b> </p> <ul> <li><a title="Add an Idea" href="ideas.php">Add An Idea</a> <br /> <li><a title="View Your Ideas" href="ideas.php">View Your Ideas</a> <br /> </li> <li><a title="View Your Offers" href="offers.php">View Your Offers</a></li> </ul> ENTREPENEUR; } //Otherwise show the Dragons' Form (On the left) else if($userlevel == "2"){ //Dragon List $page = <<<DRAGON <p><b>Welcome to the site $username</b> </p> <ul> <li>View Latest Ideas </li> <li>Make an Offer</li> <li>Review Offers </li> <li>View All Ideas</li> <li>Manage Your Profile</li> </ul> DRAGON; } //Check if login is true and show the list for Dragon // Connect to server and select databse. mysql_connect($hostname, $db_user, $db_password)or die("cannot connect"); mysql_select_db($dbname)or die("cannot select DB"); if (isset($_POST['Submit'])) { // username and password sent from signup form $username=stripslashes($_POST['username']); $password= md5($_POST['password']); $sql="SELECT * FROM $db_table WHERE username='" . mysql_real_escape_string($username) . "' and password='" . mysql_real_escape_string($password) . "'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $username and $password, table row must be 1 row if($count==1){ //Register Username and Password $_SESSION['username'] = $username; $_SESSION['password'] = $password; $a=mysql_fetch_array($result); $u = $a['userlevel']; $_SESSION['userlevel'] = $u; $_SESSION['UserID'] = $userID; header("location:login_success.php"); //If login is correct direct to login_success.php exit(); // always use the exit after a header to prevent the rest of the script from executing } else { $errormessage = "Invalid Username or Password"; } } ?> How do I output the $userID into the site, I need this so that I can input the $userID into the database, As shown by my INSERT Command, But at the moment its not inserting a value it is inserting "" Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 25, 2008 Share Posted February 25, 2008 i cannot find your insert query? --- on a side note: if you are using md5() on a variable, you dont need to use mysql_escape_string() as an md5 hash is alphanumerical. $sql="SELECT * FROM $db_table WHERE username='" . mysql_real_escape_string($username) . "' and password='" . mysql_real_escape_string($password) . "'"; could just be: $sql="SELECT * FROM $db_table WHERE username='" . mysql_real_escape_string($username) . "' and password='" . $password . "'"; -- also: $a=mysql_fetch_array($result); $u = $a['userlevel']; $_SESSION['userlevel'] = $u; $_SESSION['UserID'] = $userID; could just be: $a=mysql_fetch_array($result); $_SESSION['userlevel'] = $a['userlevel']; $_SESSION['UserID'] = $a['id']; $_SESSION['UserID'], im not sure where your getting $userID from, im guessing this is your problem area from the code you have given. the above code assumes the "User ID" you want is in a field called "id", if its anything else change 'id' to your field name. hope this helps, Quote Link to comment Share on other sites More sharing options...
stublackett Posted February 25, 2008 Author Share Posted February 25, 2008 Apologies There is no INSERT Command, Thats all dealt with correctly on another page It seems that the $_SESSION['userID']; is not cascading out to other pages The login.php page is coded like this : <?php session_start(); include ("dbconnect.php"); $message = $_GET['message']; //Show LoginForm using HEREDOC $page = <<<LOGINFORM <form method="post" action="" login=true"> <fieldset> <div> <label for="username">User Name :</label> <input type="text" name="username" id="username" class="txt" /> </div> <div> <label for="fullname">Password : </label> <input type="password" name="password" id="password" class="txt" /> </div> </fieldset> <div> <input type="submit" name="Submit" id="Submit" value="Login" class="btn" /> </div> </fieldset> </form> LOGINFORM; $username = $_SESSION['username']; $userid = $_SESSION['userID']; $userlevel = $_SESSION['userlevel']; //If Userlevel is 1 show the Entrepeneurs' Form (On the left) if($userlevel == "1"){ //Entrepeneur List $page = <<<ENTREPENEUR <p><b>Welcome to the site $username</b> </p> <ul> <li><a title="Add an Idea" href="ideas.php">Add An Idea</a> <br /> <li><a title="View Your Ideas" href="ideas.php">View Your Ideas</a> <br /> </li> <li><a title="View Your Offers" href="offers.php">View Your Offers</a></li> </ul> ENTREPENEUR; } //Otherwise show the Dragons' Form (On the left) else if($userlevel == "2"){ //Dragon List $page = <<<DRAGON <p><b>Welcome to the site $username</b> </p> <ul> <li>View Latest Ideas </li> <li>Make an Offer</li> <li>Review Offers </li> <li>View All Ideas</li> <li>Manage Your Profile</li> </ul> DRAGON; } //Check if login is true and show the list for Dragon // Connect to server and select databse. mysql_connect($hostname, $db_user, $db_password)or die("cannot connect"); mysql_select_db($dbname)or die("cannot select DB"); if (isset($_POST['Submit'])) { // username and password sent from signup form $username=stripslashes($_POST['username']); $password= md5($_POST['password']); $sql="SELECT * FROM $db_table WHERE username='" . mysql_real_escape_string($username) . "' and password='" . mysql_real_escape_string($password) . "'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $username and $password, table row must be 1 row if($count==1){ //Register Username and Password $_SESSION['username'] = $username; $_SESSION['userID'] = $userid; $_SESSION['password'] = $password; $a=mysql_fetch_array($result); $u = $a['userlevel']; $_SESSION['userlevel'] = $u; header("location:login_success.php"); //If login is correct direct to login_success.php exit(); // always use the exit after a header to prevent the rest of the script from executing } else { $errormessage = "Invalid Username or Password"; } } ?> Thanks for the suggestion, But at the present moment I just want that $userID outputting The page that I want it to output into is login_success.php and its scripted as follows : <?php session_start(); include ("dbconnect.php"); if(!isset($_SESSION['username'])){ $message = <<<HTML This page is for Registered users only <br /> <br /> Register to the site, <br /> <br /> By following the link in the menu on the left HTML; $page = <<<LOGINFORM <form method="post" action="" login=true"> <fieldset> <div> <label for="username">User Name :</label> <input type="text" name="username" id="username" class="txt" /> </div> <div> <label for="fullname">Password : </label> <input type="password" name="password" id="password" class="txt" /> </div> </fieldset> <div> <input type="submit" name="Submit" id="Submit" value="Login" class="btn" /> </div> </fieldset> </form> LOGINFORM; }else{ //Protected Area sits below $message = "Welcome to the Dragons Den ".$_SESSION['username']; } $username = $_SESSION['username']; $userlevel = $_SESSION['userlevel']; $userid = $_SESSION['userID']; //If Userlevel is 1 show the Entrepeneurs' Form (On the left) if($userlevel == "1"){ //Entrepeneur List $page = <<<ENTREPENEUR <p><b>Welcome to the site $username</b> </p> <ul> <li><a title="Add an Idea" href="ideas.php">Add An Idea</a> <br /></li> <li><a title="View Your Ideas" href="ideas.php">View Your Ideas</a> <br /> </li> <li><a title="View Your Offers" href="offers.php">View Your Offers</a></li> </ul> ENTREPENEUR; } //Otherwise show the Dragons' Form (On the left) else if($userlevel == "2"){ //Dragon List $page = <<<DRAGON <p><b>Welcome to the site $username</b> </p> <ul> <li>View Latest Ideas </li> <li>Make an Offer</li> <li>Review Offers </li> <li>View All Ideas</li> <li>Manage Your Profile</li> </ul> DRAGON; } ?> Quote Link to comment Share on other sites More sharing options...
vicodin Posted February 25, 2008 Share Posted February 25, 2008 What you need to do is when you authenticate the user and the user is good: $_SESSION['userid'] = $_POST['username']; Quote Link to comment Share on other sites More sharing options...
stublackett Posted February 25, 2008 Author Share Posted February 25, 2008 Thats not what I'm after I'm afraid! In my Database table is a field called "UserID", So I need the "UserID" to then be cascaded in other PHP pages, Its worked well with the UserLevel field in the table but not the UserID, I have a few users set in the table, I think there currently numbered 1-9 so when a user logs in it says "Welcome <username>" and I've tried to echo "UserID" so ideally user 1 would be logged in and it says "Welcome Username 1" this just showing that the UserID is outputting through sessions to other pages from the login.php page then cascading to others The annoying thing is, I had this working, Deleted some records from my Database and I cant for the life of me get the number back in! Quote Link to comment Share on other sites More sharing options...
uniflare Posted February 25, 2008 Share Posted February 25, 2008 hmm, this line: //Register Username and Password $_SESSION['username'] = $username; $_SESSION['userID'] = $userid; $_SESSION['password'] = $password; where is $userid set? if its not set then $_SESSION['userID'] will = null. --- a way i commonly use to debug arrays like $_SESSION, is print_r();, this displays a neat little unix-formatted (\n) interpretation of an array. try using print_r($_SESSION); this will show you what variables/arrays etc are stored in the current session. hope this helps, 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.