cerberus478 Posted October 25, 2011 Share Posted October 25, 2011 I would like to know how to display the user's name after they logged in. This is the form: <form action="login" method="post" name="LoginForm"> <p> <input name="username" type="text" /></p> <p> <input name="password" type="text" /></p> <p> <input type="submit" value="Login" /></p> </form> This is the login.php <?php $myusername=$_POST['username']; $mypassword=$_POST['password']; // To protect MySQL injection (more detail about MySQL injection) $myusername = stripslashes($myusername); $mypassword = stripslashes($mypassword); $myusername = mysql_real_escape_string($myusername); $mypassword = mysql_real_escape_string($mypassword); $sql="SELECT * FROM members WHERE username='$myusername' and password='$mypassword'"; $result=mysql_query($sql); // Mysql_num_row is counting table row $count=mysql_num_rows($result); // If result matched $myusername and $mypassword, table row must be 1 row if($count==1){ // Register $myusername, $mypassword and redirect to file "login_success.php" session_register("username"); session_register("password"); header("location:success"); } else { echo "Wrong Username or Password"; } ?> This is the page that it goes to after login, success.php: <?php foreach ($this->_params['list'] as $login ){ $login_name = $login['name']; echo "<table>"; echo "<tr>"; echo "<td>"; echo "<a href=/logins/view/".$login['id'].">$login_name</a>"; echo "</td>"; echo "</tr>"; echo "</table>"; } ?> <?php session_destroy(); ?> <a href="/partner_portals/view">Log out</a> Quote Link to comment Share on other sites More sharing options...
freelance84 Posted October 25, 2011 Share Posted October 25, 2011 http://php.net/manual/en/function.session-register.php - Don't use this. http://www.php.net/manual/en/function.session-start.php - Use this to start your session on any php script your require the use of sessions with. Then set the sessions in this fashion: $_SESSION['ID'] = $username; $_SESSION['username'] = $userID; //add any more sessions you require here. //now redirect to the locations required after authentication. Quote Link to comment Share on other sites More sharing options...
Buddski Posted October 25, 2011 Share Posted October 25, 2011 $username and $password are not set in that snippet you provided. Do you really need to store their password in the session? 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.