how can i make a php script for enter username and password?
how can i make a php script for enter username and password?
Well. There are many solutions.
Here is a very unsecure and stupid (but working) way (one user only):
Put this in a file and include it in every php-file where you require a password.
I am pretty tired and wrote this just now, so please forgive me for any stupid mistakes. I bet someone can correct this.
Here is a very unsecure and stupid (but working) way (one user only):
| Code: |
| <?
session_start(); $usr = $_POST["usr"]; $pwd = $_POST["pwd"]; if ( $usr == "heja" and $pwd == "aik" ) { $_SESSION["login"] = 1; } if ( $_SESSION["login"] != 1 ) { print "<form method=\"post\"><input name="usr"><input name="pwd"></form>"; exit; } ?> |
Put this in a file and include it in every php-file where you require a password.
I am pretty tired and wrote this just now, so please forgive me for any stupid mistakes. I bet someone can correct this.
well one thing you left out is that it will work only for the user heja and the password aik... if he wants to implement a login form on a public website he will need some sql . Let`s see if i can do do better:
1)db connect file
save this as let`s say db.php
now the login form:
I would recommend reading some sessions tutorials...or you could just go to www.evolt.org/article/PHP_Login_Script_with_Remember_Me_Feature/17/60265/
and read there a very good tutorial...but it could be pretty complex if you don`t know nothing about session, cookies, and php functions
Good luck
1)db connect file
| Code: |
|
$db_user = 'mysql_username'; $db_pass = 'mysql_password'; $db_name = 'mysql_db_name'; $user_name = $_POST['user_name']; $password = $_POST['password']; //connect to the DB and select the database $connection = mysql_connect('localhost', $db_user, $db_pass) or die(mysql_error()); mysql_select_db('db_name', $connection) or die(mysql_error()); |
now the login form:
| Code: |
|
<?php //start the session (unique for each user) session_start(); //include the file for connecting to the db include('db.php'); //set up the query $query = "SELECT * FROM users WHERE user_name='$user_name' AND password='$password'"; //run the query and get the number of affected rows $result = mysql_query($query, $connection) or die('error making query'); $affected_rows = mysql_num_rows($result); //if there's exactly one result, the user is validated. Otherwise, he's invalid if($affected_rows == 1) { print 'you have logged int'; $_SESSION['user_name'] = $user_name; } else { print 'user does not exist'; } ?> |
I would recommend reading some sessions tutorials...or you could just go to www.evolt.org/article/PHP_Login_Script_with_Remember_Me_Feature/17/60265/
and read there a very good tutorial...but it could be pretty complex if you don`t know nothing about session, cookies, and php functions
Good luck
Related topics
