![]() |
shopping cart (0 item) |
||||||
|
|||||||
![]() ![]()
|
Create user login in PHPCreate members pages on your website within minutes with Member Login script. PHP is a good alternative when you decide to add a password protected web pages on your web site. You can also use htaccess password protection but with PHP you can create a lot more complex and configurable protection. In this example I will use SESSION variables for login verification. Lets start with building a configuration file for setting up all the username/password combinations. Create a new passwords.php file and add the following code in it. <?php $USERS["username1"] = "password1"; $USERS["username2"] = "password2"; $USERS["username3"] = "password3"; function check_logged(){ global $_SESSION, $USERS; if (!array_key_exists($_SESSION["logged"],$USERS)) { header("Location: login.php"); }; }; ?> Above code creates an $USER array with 3 username/password combinations. We also did a function which will be used later to check if an user is logged in or not. What we need now is a login page (called login.php) where users will enter their username and password and will login. <?php session_start(); include("passwords.php"); if ($_POST["ac"]=="log") { /// do after login form is submitted if ($USERS[$_POST["username"]]==$_POST["password"]) { /// check if submitted username and password exist in $USERS array $_SESSION["logged"]=$_POST["username"]; } else { echo 'Incorrect username/password. Please, try again.'; }; }; if (array_key_exists($_SESSION["logged"],$USERS)) { //// check if user is logged or not echo "You are logged in."; //// if user is logged show a message } else { //// if not logged show login form echo '<form action="login.php" method="post"><input type="hidden" name="ac" value="log"> '; echo 'Username: <input type="text" name="username" /><br />'; echo 'Password: <input type="password" name="password" /><br />'; echo '<input type="submit" value="Login" />'; echo '</form>'; }; ?> In order to use the user login feature for your PHP files you need to put that code at the very top of each of your PHP files that need to be protected. <?php session_start(); /// initialize session include("passwords.php"); check_logged(); /// function checks if visitor is logged. If user is not logged the user is redirected to login.php page ?> your page code goes here Create members pages on your website within minutes with Member Login script. COMMENTS
Tafseer Kindly i want to make login page for data entry form. This form will use in LAN. So every user no need to type for after every entry to login, please help me kangkan can i use this this peice of code to make a password protected html page rajagopal thank u very much for this information chel thanx .. dfgdf Hi everyone.. actually I have a problem to create specific user for my final year project. I' am doing submit drawing system for architect. my problem is, I can't do: After user A upload her/his drawing and send to user B, User B only can download the drawing from user A only..Another user can't download another user's drawing to avoid the plagiarism.. Sean Scarmack This is great. first time ive got a login to work! thank you so much for your time and information! quick question. is there a way. well there is always a way. how would i go about adding access levels. like...say user1 can access files 1,2,3. user2 can access files 1,2. and user3 can only access files 1. Joshua J Mallory Try this Log Out page <?php session_start(); include 'password.php'; check_logged(); session_unset(); session_destroy(); echo 'you are logged out'; ?> bj okay this is real cool code I like it, but I ran into this problem.. Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\ICC\login.php:9) in C:\xampp\htdocs\ICC\login.php on line 10 Warning: include(file:///C|/xampp/htdocs/ICC/passwords.php) [function.include]: failed to open stream: Invalid argument in C:\xampp\htdocs\ICC\login.php on line 11 Warning: include() [function.include]: Failed opening 'file:///C|/xampp/htdocs/ICC/passwords.php' for inclusion (include_path='.;\xampp\php\PEAR') in C:\xampp\htdocs\ICC\login.php on line 11 Warning: array_key_exists() expects parameter 2 to be array, null given in C:\xampp\htdocs\ICC\login.php on line 20 Username: Password: and exactly where do I put this script each place I put it i get an error do I put it after the <header></header> or in the <body> to protect pages...????? <?php session_start(); /// initialize session include("passwords.php"); check_logged(); /// function checks if visitor is logged. If user is not logged the user is redirected to login.php page ?> thanks...love this place Todd Thanks for this info. I got it to work when others failed. My question now is how do I create a separate thank you page so I can fire my Google Conversion code? abdul well don job, please i need more example on session and cookies POST A COMMENT
|