Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    PHP Login Not Working

    Hello, I am teaching myself web development and I am trying to allow users to log into their account. The registration process works just fine and there are no error when connecting to the database. Here is the login section of code:

    PHP Code:
    if(isset($_POST['username']) && !empty($_POST['username']) AND isset($_POST['pass']) && !empty($_POST['pass'])){
                      
    $testpass $_POST['pass'];
                      
    login(mysql_real_escape_string($_POST['username']), mysql_real_escape_string(md5($testpass)), "1645054_register");

    and here is the login function:

    PHP Code:
    function login($username$password$tableName){
        
        
    $search mysql_query("SELECT username, password, active FROM '".$tablename."' WHERE username='".$username."' AND password='".$pass."'AND active='1'") or die(mysql_error()); 
                        
        
    $match  mysql_num_rows($search);
                
        if(
    $match 0){
                    
            
    //$_SESSION = 1;
            
    echo '<meta http-equiv="refresh" content="0;home.php">';
                    
        }else{
                    
            
    $error 'Login Failed! Please make sure that you enter the correct details and that you have activated your account.';
            echo 
    $error;
                    
        }


    Any help and all help/pointers are greatly appreciated!

    Thank You

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    $search = "SELECT username, password, active FROM '".$tableName."' WHERE username='".$username."' AND password='".$password."'AND active=1";
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    New Coder
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I fixed those things and the page still does not redirect to home.php.

  • #4
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Have you verified whether your query actually returns something? If so have you made sure that your META refresh works? If not try using something like header("Location: home.php"); instead

  • #5
    New Coder
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The meta does refresh as it works on another page. This code was taken from a tutorial where is worked perfectly. Do I need to close the session or anything to have it work?

  • #6
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,211
    Thanks
    23
    Thanked 605 Times in 604 Posts
    See what your getting from the query. Add echo $match; here:
    PHP Code:
        $match  mysql_num_rows($search);
                echo 
    $match;
        if(
    $match 0){ 
    You could/should echo out the three other variables here also.

    P.S. I am more a fan of header("Location: home.php"); It wouldn't hurt to try it.
    Last edited by sunfighter; 04-04-2014 at 01:50 PM.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #7
    New Coder
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I'm not even seeing the echo displayed. If you would like to see the website for testing purposes, the link is: strings.atwebpages.com

  • #8
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    I'm noticing on your login page you have a form that looks like this:

    Code:
                
               <form>           
                <p><label class="field" for="username" method="post"> Username: </label>
                    <input class="input" name="username" value="" autocomplete="off" type="text"></p>
                   
                </form>
    In your form you have not specified neither an action or a method. The form has no idea what to do. You need to have something like

    Code:
    <form action="login.php" method="post">
    Change "login.php" to whatever php file your login script is located.
    Not only that, there is no specifiction for actionally submitting the form. You'll need an <input> element of type "submit" like this in your form:

    Code:
    <input type="submit" name="submitButton" style="display: none"/>
    The display:none part is if you do not want the button to be visible. Or you could also use Javascript to accomplish that instead.

  • #9
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    After looking at it again I just realized you have your Username input and Password input within seperate forms. That is not good. Put them both in the same form element along with that submit button. Why are you doing that anyway? I think I saw like 3 seperate forms.



    As a side-note that's not relevant to your current issue I notice you are using deprecated mysql_ functions. They are neither secure nor efficient. Use the updated mysqli_ functions or PDO and make good use of prepared statements as a guard against SQL Injection.
    Last edited by Linux_Sage; 04-05-2014 at 05:20 PM. Reason: extra info

  • #10
    New Coder
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I have multiple forms because I couldn't format the fields that way I wanted to when they were all together. And which functions are deprecated that I am using?

  • #11
    New Coder
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So I added the action into the form, ad it just redirects to that page, even when there is no input into the field, so the my log in method is still not working!?

  • #12
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    You still have them spread out across seperate forms. It is NOT going to work if you do that. Since your submit input is within it's own form it will only submit post data relevant to that form. Basically if you seperate them your $_POST variable will not contain any information and your IF statement will never pass.

    Keep it like this:
    Code:
    <form action="home.php" method="post">
               <label class="field" for="username" method="post"> Username: </label>
               <input type="text" class="input" name="username" value="" autocomplete="off"/>
               <label class="field" for="pass"> Password: </label>
               <input type="password" class="input" name="pass" value="" autocomplete="off"/>
               <input type="submit" class="login_button" value="Log In" onClick="window.location.reload()"/>
    </form>
    If you're trying to keep them on the same line avoid using the P tags as they are block-based elements. You should use CSS to handle the rest.
    Make sure your action= parameter points to the correct script. If it's the same that's currently being ran either leave it blank or use a <?php $_SERVER['PHP_SELF']; ?> call

    I was referring to all your mysq_ functions. They are deprecated and will be removed in further version of PHP. Use mysqli_ type functions or PDO.
    Last edited by Linux_Sage; 04-06-2014 at 07:53 PM. Reason: Duplicate paste

  • #13
    New Coder
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I only see mysql_ functions in my code. And I couldn't get the CSS to position the forms correctly, thats why I separated them

  • #14
    Regular Coder Linux_Sage's Avatar
    Join Date
    Mar 2014
    Location
    Sterling,VA
    Posts
    106
    Thanks
    0
    Thanked 10 Times in 10 Posts
    Sorry, I didn't catch my earlier misspelling. The mysql_ functions are the ones that are deprecated.

    So you'll just have to find out how to use CSS to position them the way you want. There are many ways to do this either through margins or vertical-aline:top or whatever. Otherwise you're login form won't work.

  • #15
    New Coder
    Join Date
    Apr 2014
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Will it only work if they are in one form? And do I just replace all mysql_ with mysqli_?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •