After toying around with many scripts, I have finally gotten a login script to work. (Note this code was taken from several different websites and modified to work for myself and my students). So here are the directions to create a login\/password page to protect your webpages.<\/p>\n
1. You need to create a table in a database that includes the columns: username and password. This is the first step and I am assuming you have done this.<\/p>\n
2. Create a form in html. Here is my form code. I call this page “login.html”<\/p>\n
<html><\/strong> 3. Next I create a login script that tests if the login is correct or not. If correct, it logs the user in. I call this page “login.php”. Please note that the 4 words in ALL caps need to be changed to match your information.<\/p>\n <?php<\/strong><\/p>\n mysql_connect(“SERVERPATHUSUALLYLOCALHOST”, “YOURUSERNAME”,<\/strong> $myusername=$_POST[‘username’];<\/strong> \/\/ To protect MySQL injection (more detail about MySQL injection)<\/strong> $mysql = “SELECT * FROM members WHERE username=’$myusername’ and password=’$mypassword’ “;<\/strong><\/p>\n $result = mysql_query($mysql) or die(“cannot execute query”);<\/strong><\/p>\n $count = mysql_num_rows($result);<\/strong><\/p>\n if($count==1)<\/strong> else<\/strong> ?><\/strong><\/p>\n 4. Next I create a welcome page once they have logged in. I call this “home.php”<\/p>\n <?php<\/strong> <!DOCTYPE html PUBLIC “-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN” “http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd”><\/strong> <body><\/strong> 5. I then create a test page so you can play around with this once uploaded online. I call this “test.php”<\/p>\n <?php<\/strong> ?><\/strong><\/p>\n <?php<\/strong> <!DOCTYPE html PUBLIC “-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN” “http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd”><\/strong> <body><\/strong> 6. Finally I create a logout page. This logs the user out. I call this “logout.php”<\/p>\n <?php<\/strong> <body><\/strong> 7. Now for every page on your site that you want to be pass protected, put this script at the very top. This script will check if the user is logged in. If logged in they can access the page. Otherwise they will be directed to the login\/register page:<\/p>\n <?php<\/strong> 8. You will probably need to create a form that lets people register but that is for another tutorial.<\/p>\n","protected":false},"excerpt":{"rendered":" After toying around with many scripts, I have finally gotten a login script to work. (Note this code was taken from several different websites and modified to work for myself and my students). So here are the directions to create a login\/password page to protect your webpages. 1. You need to create a table in […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[90],"tags":[99,100,64],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/raypastore.com\/wordpress\/wp-json\/wp\/v2\/posts\/372"}],"collection":[{"href":"https:\/\/raypastore.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raypastore.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raypastore.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raypastore.com\/wordpress\/wp-json\/wp\/v2\/comments?post=372"}],"version-history":[{"count":4,"href":"https:\/\/raypastore.com\/wordpress\/wp-json\/wp\/v2\/posts\/372\/revisions"}],"predecessor-version":[{"id":397,"href":"https:\/\/raypastore.com\/wordpress\/wp-json\/wp\/v2\/posts\/372\/revisions\/397"}],"wp:attachment":[{"href":"https:\/\/raypastore.com\/wordpress\/wp-json\/wp\/v2\/media?parent=372"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raypastore.com\/wordpress\/wp-json\/wp\/v2\/categories?post=372"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raypastore.com\/wordpress\/wp-json\/wp\/v2\/tags?post=372"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}
\n <body><\/strong>
\n <form action=”login.php” method=”post”><\/strong>
\n <p>username<\/strong>
\n <input type=”text” name=”username” id=”username”><\/strong>
\n <\/p><\/strong>
\n <p>password<\/strong>
\n <input type=”password” name=”password” id=”password”><\/strong>
\n <\/p><\/strong>
\n <p><\/strong>
\n <input type=”submit”><\/strong>
\n <\/p><\/strong>
\n <\/form><\/strong>
\n <\/body><\/strong>
\n <\/html><\/strong><\/p>\n
\n “YOURPASSWORD”);<\/strong>
\n mysql_select_db(“YOURDATABASENAME”);<\/strong><\/p>\n
\n $mypassword=$_POST[‘password’];<\/strong><\/p>\n
\n $myusername = stripslashes($myusername);<\/strong>
\n $mypassword = stripslashes($mypassword);<\/strong>
\n $myusername = mysql_real_escape_string($myusername);<\/strong>
\n $mypassword = mysql_real_escape_string($mypassword);<\/strong><\/p>\n
\n {<\/strong>
\n session_register(‘username’);<\/strong>
\n header(“location:home.php”); \/\/ put your home page neme here<\/strong>
\n }<\/strong><\/p>\n
\n echo “login fail – please click here to <a href=\\”login.html\\”>login<\/a>”;<\/strong><\/p>\n
\n session_start();<\/strong>
\n if(!$_SESSION[‘username’]) {<\/strong>
\n header(“location:login.php”);<\/strong>
\n }<\/strong>
\n ?><\/strong><\/p>\n
\n <html xmlns=”http:\/\/www.w3.org\/1999\/xhtml”><\/strong>
\n <head><\/strong>
\n <meta http-equiv=”Content-Type” content=”text\/html; charset=UTF-8″ \/><\/strong>
\n <title>Untitled Document<\/title><\/strong>
\n <\/head><\/strong><\/p>\n
\n <p>congrats you have logged in<\/p><\/strong>
\n <p><a href=”test.php”>Check out test page both logged in and out<\/a><\/p><\/strong>
\n <p><a href=”logout.php”>logout <\/a><\/p><\/strong>
\n <\/body><\/strong>
\n <\/html><\/strong><\/p>\n
\n session_start();<\/strong>
\n if(!$_SESSION[‘username’]) {<\/strong>
\n header(“location:login.php”);<\/strong>
\n }<\/strong><\/p>\n
\n echo “you logged in as “.$username;<\/strong>
\n ?><\/strong><\/p>\n
\n <html xmlns=”http:\/\/www.w3.org\/1999\/xhtml”><\/strong>
\n <head><\/strong>
\n <meta http-equiv=”Content-Type” content=”text\/html; charset=UTF-8″ \/><\/strong>
\n <title>Untitled Document<\/title><\/strong>
\n <\/head><\/strong><\/p>\n
\n <p> <\/p><\/strong>
\n <p> <\/p><\/strong>
\n <p> <\/p><\/strong>
\n <p>This is a test page so that you can see what happens on pages when users are logged in or out.<\/p><\/strong>
\n <p> <\/p><\/strong>
\n <p><a href=”logout.php”>logout<\/a><\/p><\/strong>
\n <\/body><\/strong>
\n <\/html><\/strong><\/p>\n
\n session_start();<\/strong>
\n session_destroy();<\/strong>
\n echo “successfuly logout”;<\/strong>
\n ?><\/strong>
\n <!DOCTYPE html PUBLIC “-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN” “http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd”><\/strong>
\n <html xmlns=”http:\/\/www.w3.org\/1999\/xhtml”><\/strong>
\n <head><\/strong>
\n <meta http-equiv=”Content-Type” content=”text\/html; charset=UTF-8″ \/><\/strong>
\n <title>Untitled Document<\/title><\/strong>
\n <\/head><\/strong><\/p>\n
\n <p>you have logged out<\/p><\/strong>
\n <p> <\/p><\/strong>
\n <p> <a href=”loginform.html”>log back in <\/a><\/p><\/strong>
\n <p>visit <a href=”test.php”>test <\/a>page – you should not have access now and should be redirected to login screen.<\/p><\/strong>
\n <\/body><\/strong>
\n <\/html><\/strong><\/p>\n
\n session_start();<\/strong>
\n if(!$_SESSION[‘username’]) {<\/strong>
\n header(“location:login.php”);<\/strong>
\n }<\/strong>
\n ?><\/strong><\/p>\n