![]() |
|
||||||
|
|||||||
|
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
Janice Valletta I only have one error, and thats in the header for the redirect. I amworried that somehow its not getting the second " mark. I get redirected to a null webpage. header("Location:" .$REDIRECT[$temp]); Doesnt Location have to have a second quotation? as such: header("Location: http://www.$site_registered_to"); The above code loses the last and I cant seem to add it back in. james my email is j@juicygecko.com james Ok, im lost.. i dont understand how to let the users sign up, and how to get them directed too their own page? can someone help me? Mike where do i put my page the users want to be directed to after login? Whit I have used the $REDIRECT array and the other code from Dan's post about redirecting your user to another page upon login; however, it only works for the 1 username and password I set up. If I add other usernames and passwords they do not get redirected. Instead, it says page not found and my url is listed as the login page. I'm thinking my problem has something to do with the code in the passwords or login page because it seems like once I log in, it doesn't even try to redirect me to the other site. Mike How Do You Make The User When Logged in Be Directed To A Diffrent Page? Daysha How does one's site keep data info so that when a user logs in, they have their own specifice information. For example, after a user logs in, adn they go to an email form, some of their information will already be inserted because they are logged into their account Tahnks Day ------------------------ Veselin: you need to create user profile for each register user and store it into a database. Then when the user logs in, your PHP script should read user profile data and display it on a web page. dan Excellent script. Does anyone know how to give the script an individual url for each user to be redirected to upon login. Thanks for help!!! -------------------- Veselin: you can create an array with redirect URLs $REDIRECT["username1"] = "http://www.url1.com"; $REDIRECT["username2"] = "http://www.url2.com"; $REDIRECT["username3"] = "http://www.url3.com"; and in login.php after $_SESSION["logged"]=$_POST["username"]; add this $temp = $_SESSION["logged"]; header("Location: ".$REDIRECT[$temp]); -------------------- Dan: thanks veselin! Im a noob when it comes to php! So in which file and where do i insert: $REDIRECT["username1"] = "http://www.url1.com"; .... Thanks in advance!! -------------------- Veselin: in passwords.php -------------------- Dan: sorry. :( im such an idiot that i still cant figure out where to put it.... can you please be more specific? thanks again, -------------------- Veselin: this is the file where your $USERS array is -------------------- Dan: yer sorry.... i should have been more specific with my question!! lol. i know which file to put it into, but after which line of code should it be inserted? -------------------- Veselin: right after $USERS array -------------------- Dan: Is there something that i just dont understand? Ive inserted all of the code correctly yet still no joy? Do I need to enter any more code? noooob!!! -------------------- Veselin: unfortunately, I cannot provide you with free help any more. My hourly rate is $50 and I can make the scripts work on your website Daysha Also, to echo Akshaye's question....is there a way to send an email and have a link on the page if they have forgotten their password? --------------------------------- PHPjabbers.com: we can make a forgot password feature for your script but it will cost $50 Romu "For the redirect you can use header() function" When I change that, it redirects straight to chosen page (.htm) without asking any username or password. If I put therre .php-file, then I get the an error message from the browser, that the server in the address is redirecting endlesly. Please help.. ------------ Veselin: You need to redirect to a PHP file and put check_logged(); function at the top. Which file do you put the redirect into? POST A COMMENT
|