user.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
<?php class User { //private variabel for session data private $user; //function for check username and password function check_login($username, $password) { //get username value and save to private variabel $this->user = $username; //checked username and password if($username == "admin" && $password == "password") { //if username and password true, then create session. $_SESSION['login'] = true; $_SESSION['id'] = $this->user; return true; } else { //if username and password false return false; } } //function for set cookies 1 hour function setCookies($user) { setcookie("user", $user, time()+3600); } //function for checking cookies function checkCookies() { if(isset($_COOKIE)) { if(isset($_COOKIE['user'])) { $_SESSION['login'] = true; $_SESSION['id'] = $_COOKIE['user']; } } } //function for checking session function checkSession() { //if user has login and session has not been removed if(isset($_SESSION['login'])) { //redirect to file admin.php and don't need login again header("location: admin.php"); } } //function for delete sessions function session_logout() { unset($_SESSION['login']); //delete all sessions session_destroy(); //delete cookie setcookie("user", "", time()-3600); //redirect to form login header("location: index.php"); } } ?> |
main.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?php //if we use session, use this code session_start(); //include class.php file require_once "class.php"; //instance objek, because we use OOP $user = new User(); //checking cookies $user->checkCookies(); //checking session $user->checkSession(); //if file index.php get post data if($_POST) { //checked that post using function check_login (from file class.php) $login = $user->check_login($_POST['username'], $_POST['password']); //if checklist remember me if(isset($_POST['remember'])) { //setcookies $user->setCookies($_POST['username']); } //if user has login if($login) { //if true, redirect to admin.php header("location: admin.php"); } else { //if false, we must login again. $error = "Sorry username or password is wrong!!"; } } ?> |
login.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html> <head> <title>Form Login</title> </head> <body> <h1>Form Login</h1> <p><?php echo (isset($error)) ? $error : ""; ?></p> <form method="post" name="login"> <label>Username</label><br /> <input type="text" name="username" required /><br /> <label>Password</label><br /> <input type="password" name="password" required /><br /> <input type="checkbox" name="remember" />Remember me<br /> <input type="submit" value="Login" /> <button type="reset">Cancel</button> </form> </body> </html> |
check.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php //if we use session, use this code session_start(); //include file user.php require_once "user.php"; //instance objek user from class.php $logout = new User(); //if user open this page and haven't login, redirect to login form if(!isset($_SESSION['login'])) { header("location:index.php"); } //if user click logout link if($_GET && $_GET['page']) { $logout->session_logout(); } ?> |
admin.php
1 2 3 4 5 6 7 8 9 10 |
<!DOCTYPE html> <html> <head> <title>Admin Page</title> </head> <body> Hello <b><?php echo $_SESSION['id'] ?>!</b><br /> <a href="?page=logout">Logout</a> </body> </html> |
Link exchange is nothing else however it is
just placing the other person’s webpage link on your
page at appropriate place and other person will also
do similar for you.
Excellent post. I was checking constantly this blog and I am impressed!
Extremely useful info specifically the closing
phase 🙂 I care for such information much. I used to be seeking this certain info for a very
lengthy time. Thanks and good luck.
If you would like to improve your knowledge just keep visiting this web page and be updated with the most recent news posted here.
I was wondering if you ever thought of changing the page layout of your blog?
Its very well written; I love what youve got to say.
But maybe you could a little more in the way of content so people could connect with
it better. Youve got an awful lot of text for only having 1
or 2 images. Maybe you could space it out better?
Here is my website :: cialis
You ought to be a part of a contest for one of the highest quality sites on the web.
I am going to recommend this site!
my blog post: cialis
Attractive element of content. I just stumbled upon your blog and in accession capital to assert that I get actually loved account your weblog posts.
Anyway I’ll be subscribing for your feeds and even I achievement
you get right of entry to persistently rapidly.
Also visit my web page … site