Capture specific values from SQL query using PHP
so I'm new to PHP, and I wanna understand the basic server authentication process for a web login form.
- User types in details into the input elements
- Javascript to validate content typed in
- Here is where it gets blurry - PHP takes the value of the input fields, and checks if there's a similar username in the DB...
- If there is, then it compares the passwords, and if there's a match, proceed, otherwise trigger a javascript function to show 'wrong credentials' to the user
Now, for step 3:
I know I need to use PHP to execute a SQL query that goes like:
SELECT * FROM users WHERE user_name like $input开发者_如何学Go_user
But what do you do about this? I seem to not understand the basics here, I think I need a tutorial. :-(
You need to authenticate the user with his username and password.
$result = mysql_query("SELECT * FROM users WHERE username='$username' and passwoed='$password'");
$fetch = mysql_array_fetch($result);
if(mysql_num_rows($result)==1) {
$_SESSION["member_id"] = $fetch["id"];
}
Now the $_SESSION["member_id"] is available in all your pages. Look at the below link on more information about this.
http://www.tutorialized.com/tutorial/Create-a-Log-in-out-System-or-Script-for-a-Webpage.-UPDATED/69498
http://www.codewalkers.com/c/a/Miscellaneous/Writing-a-Basic-Authentication-System-in-PHP/
Of course you coulda easily googled this out!
I hope this can help you about checking the username :
$query=mysql_query("SELECT * FROM user WHERE username='$username' AND password='$password'");
$query=mysql_num_rows($query);
if($query==0) { echo "Wrong username or password."; }
else { //do what you want }
And you can search some tutorial from http://www.w3schools.com/ or http://www.google.com
精彩评论