Q&A for How to Create a Secure Login Script in PHP and MySQL

Return to Full Article

Search
Add New Question
  • Question
    How do I add privileged users only access? Or just for a specific user to access a specific page?
    Community Answer
    Community Answer
    You could add an extra value to each of your users in the database which contains their rank. Then, check their rank when they access the page and handle accordingly. However, if you only want a certain user to access a page you could also check for their username.
  • Question
    How can I create a password recovery script for the salted hashed password in case the user forgets?
    Community Answer
    Community Answer
    I recommend against reverse engineering the password -- too risky. Instead, try generating a random password, then update the user's password and send an email "with a link to a special registration page" to the email address on file (don't forget the temporary password). Add "old password" to the input fields and validate that against the database, then pass the new password as a post to registration. If you want to get fancy, you can create a separate "users" table in the database with code and a password with a challenge question. Successful login will update the real user password.
  • Question
    I've downloaded the Github project and the register works fine, but when I log in it says: "Database error: cannot prepare statement." I am using cPanel powered by GoDaddy.
    Dan Trimble
    Dan Trimble
    Community Answer
    The Github code is different from the code in the tutorial above. In the functions.php you pulled from Github, replace the login function with the one from the tutorial above -- just copy and paste it. That fixed the same problem for me.
  • Question
    Has anyone figured out the $_SESSION being lost yet?
    Community Answer
    Community Answer
    This has to do with their sec_session_start() function. If you replace that function call with the regular session_start() function, your code will work. I couldn't figure this out for the longest time. I don't know what is wrong with their custom function, but I can't make it work.
  • Question
    I have copied the code in all sections above and have managed to get the login function working correctly; however, the register section does not. What should I do?
    Community Answer
    Community Answer
    Check the file names and the database names as well -- it's quite possibly just a simple word mistake.
  • Question
    There's a problem in sec_session_start() the session_set_cookie_params causes the session to not work at all. What is missing?
    Lambertsoftware
    Lambertsoftware
    Community Answer
    Set $secure = true only if you are using a secured server (SSL). Set it to false if you are using an unsecured server.
  • Question
    I took the exact steps from this guide. I can register new users but there is one problem: When I try to log in with the correct login details, I get the message on the protected_page that I'm not authorized to view that page.
    Community Answer
    Community Answer
    Go to the given Github project link and download the project. Then replace the functions.php code to the latest version.
  • Question
    I'm having the same problem with not being authorized to view the protected_page. I downloaded the latest from GitHub and get an error of: "Database error: cannot prepare statement". Could it be that an extra variable called salt is included in the newer function.php?
    Community Answer
    Community Answer
    You may be using the database members table without the updated $salt. This $salt is used in the login() function in the functions.php. If you add the $salt to your table it should work.
  • Question
    Can you add links for these files?
    Community Answer
    Community Answer
    Yes, if you use href, you can make a link that redirects you to the page you want it to redirect to.
  • Question
    Which version of PHP is required to use the hash_equals() function?
    Community Answer
    Community Answer
    PHP 5 >= 5.6.0, PHP 7 There are functions available that replicate the behavior for older versions. Google it.
  • Question
    I followed the steps and got the error code: "Fatal error: Call to a member function close() on a non-object in /.../includes/register.inc.php on line 46". What does this mean?
    Community Answer
    Community Answer
    Check if you have placed the JS correctly. If you did, check the names of the files. Maybe see if you have added extra field in the DB or modified the pw field. Same to do with the email. Make sure these exist include_once 'db_connect.php'; include_once 'psl-config.php'; and also if you have added extra field add these at line 8 and the bottom of the page. Also check the path.
  • Question
    I have registered several different users, but when I try to test the login feature it says, "You are not authorized to access this page. Please login." What am I doing wrong?
    Community Answer
    Community Answer
    Try using a different browser. I have found issues with Internet Explorer on pages that worked fine with Chrome. If that fails, check for typos. Another option is to break the page in reverse. Set the Login check == False. This will display the protected page, and you can work through any additional bugs.
  • Question
    Is there a way to make the session last longer? It keeps logging me out every hour or so.
    Community Answer
    Community Answer
    In your php.ini file, change the value of "session.gc_maxlifetime". It's the time in seconds before the session gets destroyed.
  • Question
    I followed all the steps I got from registering, but I have a Database error: cannot prepare statement. What am I doing wrong?
    Community Answer
    Community Answer
    If you used the package from the Github project link, replace the login function in function.php with the one as shown above.
  • Question
    There seems to be a problem with this code in the functions.php page with the login function around line 53 or 54 if you paste the login function in underneath the sec_session_start function. It says there is an unexpected '{' in the if statement containing the password_verify function. I have been trying to solve this problem for a few days now and I have been unable to figure out what is wrong. My code is virtually identical to your code. All of the {}'s seem to have opening and closings. I'm really stumped. What should I do?
    Community Answer
    Community Answer
    This is because this tutorial is incomplete, and there is no "password_verify" function. This will have to be built by the developer!
  • Question
    When I log in with the correct password and email, I still get the message, "You are not authorized to access this page. Please log in." Do you have tips for what to try next?
    Community Answer
    Community Answer
    Check if session is set with var_dump($_SESSION). If not, check if you have start the session with sec_session_start().
  • Question
    I basically copied everything and I organized the directories as suggested, but my login keeps giving me the message, "Error Logging In!", in my URL: error=1. What could that be?
    Community Answer
    Community Answer
    Check the Process-login.php to make sure the variables are being passed properly. Change line header('Location: ../index.php?error=1'); to header('Location: ../index.php?error=1['.$email.', '.$password.']'); [ & ] are there just to highlight the information. It will print [] if variables are empty. It would be necessary to have additional information to be more helpful.
Ask a Question
200 characters left
Include your email address to get a message when this question is answered.
Submit

Return to Full Article

You Might Also Like