Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 273589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:28:47+00:00 2026-05-12T00:28:47+00:00

Can anyone see anything wrong with this login script: public function login($username, $pass, $remember)

  • 0

Can anyone see anything wrong with this login script:

public function login($username, $pass, $remember) {
  // check username and password with db
  // else throw exception  
  $connect = new connect(); 
  $conn = $connect->login_connect();      
  // check username and password
  $result = $conn->query("select * from login where
                          username='".$username."' and
                          password=sha1('".$pass."')");
  if (!$result) {
    throw new depException('Incorrect username and password combination. Please try again.');
  } else {
    echo $username, $pass;
  }

To explain:

At the moment the script is allowing anything through. In other words the query is returning true for any username and password that are passed to it.
I’ve put the echo statement just as a check – obviously the script would continue in normal circumstances!

I know that the connect class and login_connect method are working because I use them in a register script that is working fine. depException is just an extension of the Exception class.
The function login() is part of the same class that contains register() that is working fine.
I know that the two variables ($username and $pass) are getting to the function because the echo statement is outputting them accurately. (The $remember variable is not needed for this part of the script. It is used later for a remember me process).
I’m stumped. Please help!

UPDATE

Thanks for those responses. I was getting confused with what the query was returning. The complete script does check for how many rows are returned and this is where the checking should have been done. Everything is now working EXCEPT for my remember me function. Perhaps someone could help with that?!?! Here is the full script:

public function login($username, $pass, $remember) {
  // check username and password with db
  // else throw exception  
  $connect = new connect(); 
  $conn = $connect->login_connect();      
  // check username and password
  $result = $conn->query("select * from login where
                          username='".$username."' and
                          password=sha1('".$pass."')");
  if (!$result) {
    throw new depException('Incorrect username and password combination. Please try again.');
  }       
  if ($result->num_rows>0) {
    $row = $result->fetch_assoc();
    //assign id to session
    $_SESSION['user_id'] = $row[user_id];        
    // assign username as a session variable
    $_SESSION['username'] = $username;        
    // start rememberMe
    $cookie_name = 'db_auth';
    $cookie_time = (3600 * 24 * 30);*/ // 30 days
    // check to see if user checked box
    if ($remember) {
      setcookie ($cookie_name, 'username='.$username, time()+$cookie_time);
    }
    // If all goes well redirect user to their homepage.
    header('Location: http://localhost/v6/home/index.php');   
  } else {
    throw new depException('Could not log you in.);
  }
}

Thanks very much for your help.

UPDATE 2!

Thanks to your help I’ve got the main part of this script working. However, the remember me bit at the end still doesn’t want to work.
Could someone give me a hand to sort it out?
$username, $pass and $remember are all short variable names that I assigned before passing them to the function to save writing $_POST[‘username’] etc. everytime. $remember refers to a checkbox.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T00:28:47+00:00Added an answer on May 12, 2026 at 12:28 am

    What does $conn->query() return, a MySQL resource object like mysql_query() does? If so then it’ll always compare "true". mysql_query() only returns FALSE if the query completely fails, like it has a syntax error or a table doesn’t exist.

    To check if you got any results you need to try to fetch a row from the result set and see if you get anything, via whatever your equivalent of mysql_fetch_row() is.

    Important: Your script is vulnerable to SQL injection attacks, or even just odd usernames like o'neil with an apostrophe. You should escape all variables in a query with mysql_real_escape_string() (or equivalent) to make sure your query doesn’t get messed up by special characters. Or, even better, use prepared statements which look like

    select * from login where username=? and password=sha1(?)
    

    Re: UPDATE

    Variables from a form are available via either $_GET or $_POST, depending on which method was used to submit the form. Try if (isset($_POST['remember'])) to see if that check box was checked.

    Important: I see that you tried to use a bare $remember to see if the check box was checked. That suggests to me that you are trying to take advantage of the register_globals feature in PHP which makes your GET and POST variables accessible via regular variable names. If that is the case you should heed the warning in the PHP manual!

    WARNING

    [register_globals] has been DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged.

    Use $_GET and $_POST instead. I could tell you how to make if ($remember) work, actually, but given the inherent evil-ness of register_globals I’m not gonna! 😉

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 195k
  • Answers 195k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You can do this by joining the table on itself.… May 12, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer In this case, you can use the ExecuteScalar() method of… May 12, 2026 at 7:03 pm
  • Editorial Team
    Editorial Team added an answer I've had the best luck just doing $d = new… May 12, 2026 at 7:03 pm

Related Questions

My 7 year old would like to learn, how to program? (his idea not
When I try to post a file its coming back false ie there was
I have this code to show a map using the Virtual Earth API: <script
I am attempting to load document files into a document library in SharePoint using

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.