mysql - How to set user/admin accounts for a redirect after log in? PHP -


My problem with this code is that the IF statement which determines which page to go index Php seems to default i have already created a login table in mysql and the username / password column, and the second column with a boolean value indicating that the user is the administrator.

  session_start (); // Start Session $ error = ''; // blank ($ _ POST ['username']) empty ($ _ POST ['password'])) {$ error = "username or password is invalid"; } Else {// Define $ username and $ password $ username = $ _POST ['username']; $ Password = $ _POST ['password']; // server_name, user_id and password as a parameter to establish a connection with the server via $ connection = mysql_connect ("", "", "", "", ""); // Select the database $ db = mysql_select_db ("", $ connection); // Register: Finds the queries of the users to bring SQL queries and user matches. $ Query = "SELECT * login from WHERE username = '$ username' and password = '$ password'"; $ Result = mysql_query ($ query) or die (mysql_error ()); $ Line = mysql_fetch_array ($ result); $ Count = mysql_num_rows ($ result); $ Auth = $ row ['Admin']; If ($ count == 1) {if ($ auth ['admin'] == 1) {session_start (); $ _SESSION ['Admin'] = $ auth; $ _SESSION ['username'] = $ username; Header ("location: member.php"); } Otherwise ($ auth ['admin'] == 0) {session_start (); $ _SESSION ['Admin'] = $ auth; Header ("location: index.php"); }} And {$ error = "username or password is invalid"; } Mysql_close ($ connection); // Close connection}}  

I have written your login script to use it. I think that you will find what it will be better for you.

  if (isset ($ _ POST ['username'])) {$ username = stripslashes ($ _ POST ['username']); $ Username = strip_tags ($ username); $ Username = mysql_real_escape_string ($ username); $ Password = $ _POST ['password']; // $ password = MD5 ($ password); $ Db_host = "host"; $ Db_username = "username"; $ Db_pass = "password"; $ Db_name = "db_name"; Mysql_connect ("$ db_host", "$ db_username", "$ db_pass"); Mysql_select_db ("$ db_name"); // Join your database, if the user name is $ sql = mysql_query ("SELECT * FROM login WHERE usernames = '$ username' and password = '$ password'" is set); $ Login_check = mysql_num_rows ($ sql); If ($ login_check> 0) {// if the session is on, after the session is started; // Start Session here (only once) ($ line = mysql_fetch_array ($ sql)) {// user administrator query $ auth = $ row ['admin']; $ _SESSION ['Admin'] = $ auth; // Set Administrator session variable $ _SESSION ['username'] = $ username; // Set user name session variable if ($ auth == 1) {header ("location: member.php"); // if the user is at 1, send it to member} and if ($ auth == 0) {header ("location: index.php"); // If user is at 0, then send to index} exit (); }} And {header ('location: login.php'); // If the user does not exist, reload the login page. } Mysql_close (); }  

I recommend using MD5 hash password.

When a person registers your site, you pass the password to MD5 hash this line $ password = MD5 ($ password); Before DB Entry

About your $ AUTH, above it assumes that your entry in the database is either 0 or 1. By the way, if you are controlling it, I recommend using enum in the SQL database. Set type "enum" and type to '0', '1'


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -