Jump to content

Recommended Posts

I'm not quite sure what is wrong with this code.  It is all the more frustrating because it works fine when implemented on my localhost, but once I put it live on the web, it fails.

 

It is a simple login check, but when I try to submit my information, every single time the error "You must enter a username" comes up.  However, I have entered text into the username box.  I have no idea what could be causing this!

 

The functions

                <h3>Login</h3>
                
                
                <?php
                if(isset($_POST['login'])) // Has information been entered?
                {
                    $username = mysql_real_escape_string($_POST['username']);
                    $password = mysql_real_escape_string($_POST['password']);
                    $error = login($username, $password);
                    
                    $error = isset($error) ? $error: ""; 
                    echo $error; // Report login status
                }
                
                if(isset($_SESSION['userid'])) { // Is user already logged in? 
                    header('Location:/');
                 } else { // If not, display login form ?>
                <!-- Login Form -->
                <form class="prettyform" action="" method="post">
                    <p>Username: <input type="text" name="username"></input></p>
                    <p>Password: <input type="password" name="password"></input></p>
                    <input type="submit" value="Login" name="login">
                </form>
                <?php } 


function login($username, $password) // CHECKS USER LOGIN
    {
        
        if ($username != NULL) // Username entered?
   {
       
       if ($password != NULL) //Password entered?
       {
      if (existing_username($username) == TRUE) // Does username exist?
      {
          if (valid_match($username, $password) == TRUE) // Is entered information valid?
          {
         $error = "You have successfully logged in!";
                        
                        $query = mysql_query("SELECT * FROM users WHERE username = '$username'");
                        $userid = mysql_fetch_array($query);
                        
                        $_SESSION['userid']=$userid['userid']; // Set session variable
          }
          else
                    {
                    $error = "<br>That username/password combination is invalid.";
          }
      }
      else
      {
      $error = "<br>That username does not exist.";
      }
       }
       else
       {
      $error = "<br>You must enter a password";
       }
   }
   else
   {
       $error = "<br>You must enter a username";
   }
        
        return $error;
    } ?>

 

Any help would be appreciated!  Let me know if you need to see anything more =]

It is this page: http://dellusionary.com/login.php

Link to comment
https://forums.phpfreaks.com/topic/263346-form-data-doesnt-seem-to-be-posting/
Share on other sites

where are you defining existing_username() and valid_match() ?

 

Also, I *assume* that valid_match and existing_username() both perform db queries...and then you have yet another query to get and assign user id to a session var...why not do a single query?

 

Here are those two functions.  The if wasn't even getting that far so I figured they couldn't be the problem.  As far as streamlining into one query, I am hacking the basics out before I go back and really get everything composited into neater style ^^.  I have a lot of work ahead of me!  That is a good idea though.

 

(code tags failed for some reason)

 

<?php

    function existing_username($username) // CHECKS IF USERNAME EXISTS

    {

        $query = mysql_query("SELECT * FROM users WHERE username = '$username'" );

        $result = mysql_num_rows($query);

       

        return !($result == 0);

       

    }

   

    function valid_match($username, $password) // CHECKS IF LOGIN VALID

    {

$encryptpass = md5($password.$username);

        $query= mysql_query("SELECT * FROM users WHERE username = '$username'AND password ='$encryptpass'");

        $result = mysql_num_rows($query);

       

        return ($result == 1);

       

    }

?>

Have you done any debugging on your own?  If the form data doesn't seem to be posting, print out the post data and see for yourself.  Your username isn't being set, so echo it out and check it.  You cannot possibly fix a script simply by staring at it.

It's definitely the form data, it doesn't echo out. the submit data obviously passes, however, because the function runs.  No clue.

 

EDIT: It may have something to do with the fact that it works on localhost and not on the host server.  Does anyone have any idea what could cause that?

so at the very top of your script, outside of all conditions, if you put the following:

 

echo "<pre>";
print_r($_POST);
echo "</pre>";

 

You see the data you submitted?

 

If yes...okay, where in your script do you actually connect to your database?  I see a whole lot of database queries happening... do you actually connect to/select your database somewhere?  Did you test your queries to see if they are returning what they are supposed to be returning, or if they are throwing an error? (turn error reporting on by adding error_reporting(-1); to top of script or  echo mysql_error after each query). [/code]

I thought that might be the problem, but I see nothing wrong with my connection query.  I've checked over the spelling 5-6 times, and the syntax is the same when I connect to localhost.  That is the most obvious answer, though, so I will check again

 

looking at the syntax helps, but is not good enough.  So you spelled it right...so what? Do you actually have a database on your server, did you check that?  Do you have error reporting turned on?  Are you checking the results of your query?  You can't just eyeball!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.