treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Help with PHP Login

  • Hello css-tricks community.

    I am trying to work on a little area for a website where the administrator can login and update a php table that offers 3 services. I have the php table working and updating properly. However when I did the login page. My page will not redirect to the page that I select. I used the [phpeasy steps login tutorial] but I cannot figure out why it is not working. I can get get it to tell me if the user name and password is wrong and also I can successfully sign out. Is there another tutorial I could use, or could you suggest what is wrong with my code. I studying php and I am trying to learn what is wrong with it. Any help will be greatly appreciated!!!** Thank you in advance!** This is for the login page codepen for login page...

    This is for the login action php:

    <?php ob_start(); ?>
    

    <?php include 'include.php'; ?> <?php // Connect to server and select databse. mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); // Define $myusername and $mypassword $myusername=$_POST['myusername']; $mypassword=$_POST['mypassword']; // 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 $tbl_name 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('myusername'); session_register('mypassword'); header('Location: http://www.mywebsite.com/php/success_login.php'); } else { echo "Wrong Username or Password"; } ob_end_flush(); ?>

                                (http://www.phpeasystep.com/phptu/6.html "http://www.phpeasystep.com/phptu/6.html") 
    
  • Something I noticed in your example code...

    This comment has "login_success.php"

      // Register $myusername, $mypassword and redirect to file "login_success.php" 
    

    But this line has "success_login.php"

      header('Location: http://www.mywebsite.com/php/success_login.php')
    

    Could this be the issue?

  • Thank you so much for your response hotpink! I will do that and look at it and see if it is the problem. Thank you for taking time to check the code for me =) I really appreciate it

  • I just checked and that is not it =(.

  • I wouldn't recommend using mysql_connect for your login. You would be much better off using PDO. Here's a tutorial on how to use it:

    PDO tutorial