Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Very Basic login php script

Options
  • 19-04-2015 4:34pm
    #1
    Registered Users Posts: 2,827 ✭✭✭


    below is some php for a basic login script to a website(local host)

    after hours of debuging , i think the reason i cant get it working is that the database isent being selected and hence cant run the select statement which is returning false
    connection is working fine

    i was following this
    http://www.phpeasystep.com/phptu/6.html

    In mysql , assignment is the db and login is the table
    table has 2 col and 1 entry
    username and password both have the value of test.
    a html form is sending the username and password and done an echo test and they are coming in grand

    not worried about any sort of encryption

    <?php
      
      
     $link = mysqli_connect("localhost", "root", "", "assignment");
      
     // username and password sent from form
     $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 login WHERE username='$myusername' and password='$mypassword'";
     $result=mysql_query($sql);
      
     // Mysql_num_row is counting table row
      
     // If result matched $myusername and $mypassword, table row must be 1 row
      
     if(mysql_num_rows($result) ==1 ){
      
     // Register $myusername, $mypassword and redirect to file "login_success.php"
     session_register("myusername");
     session_register("mypassword");
     header("location:login_success.php");
     }
     else {
     echo "Wrong Username or Password";
     }
     ?>
    

    Really appreciate a bitta help on this. Tearing my hair out looking at it


Comments

  • Registered Users Posts: 1,468 ✭✭✭Asmooh


    You are using mysqli and mysql both, choose 1!!!
    seems your connection is also not really good check below mine.


    dbdata.php:
    <?php
    session_start();
    $DBServer = 'localhost';
    $DBUser   = '********';
    $DBPass   = '********';
    $DBName   = '********';
    $conn = new mysqli($DBServer, $DBUser, $DBPass, $DBName);
    $connlink = mysqli_connect($DBServer, $DBUser, $DBPass, $DBName);
    // check connection
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    if ($conn->connect_error) {
    
      trigger_error('Database connection failed: '  . $conn->connect_error, E_USER_ERROR);
    
    }
    ?>
    

    This is one I use for testing (it doesnt encrypt in the code below)
    <?php
    
    require("$_SERVER[DOCUMENT_ROOT]/dbdata.php");
    
    $postdatauser = mysqli_real_escape_string($conn, $_REQUEST['user']);
    $postdatapass = mysqli_real_escape_string($conn, $_REQUEST['pass']);
    $postdataterm = mysqli_real_escape_string($conn, $_REQUEST['terms']);
    
    if($postdataterm == "false") { $postdataterm = "0"; } else { $postdataterm = "1"; }
    
    $getuser = $conn->query("SELECT vipuserid,vippassword,vipaccountid,vipaccountkey FROM vip WHERE vipuserid = '$postdatauser' AND vippassword = '$postdatapass'");
    $checkuser = mysqli_num_rows($getuser);
    
    if($checkuser == "0") {
    }
    
    	else
    
    {
    
    while($putuser = mysqli_fetch_array($getuser)) {
    // login OK
    }	
    
    	
    
    }
    
    
    mysqli_close($connlink);
    
    ?>
    


  • Registered Users Posts: 200 ✭✭druidhill


    I'd be inclined to stop the way you are going and use PDO instead, but if you are going to use a tutorial like that, stick to it - you seem to have changed things around and when you are learning that is not a good idea.

    You'll also need to learn how to debug / troubleshoot, so spend time learning it now. Work through each step you do and ensure it worked so you can rule that out and move on to the next step. It might take some time now, but you will get proficient very quickly.

    Probably best to go back to basics and spend some time at getting a good foundation at the various pieces first (e.g. not connecting to MySQL as root and root password seems to be blank).


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    druidhill wrote: »
    I'd be inclined to stop the way you are going and use PDO instead, but if you are going to use a tutorial like that, stick to it - you seem to have changed things around and when you are learning that is not a good idea.
    Certainly changing things around before you get it working may not be a good idea, but 'hacking code' has been the way that countless developers have learned how to develop.


Advertisement