PHP login script in a separated file -


i have been developing following php script (+ sqlite database) create login web.

up had used 1 php file, want use different files login , protected contents, mean, used have web in 1 file php (contents , password script together) want detach in different php files (one login, login.php, , other phps protected: index.php, calendar.php...)

i used code password-protect php content:

<?php require_once "login.php"; ?> 

but doesn't seem work: displays form login next content wanted protect.

this php script i'm using login.php:

<?php  $db = new pdo('sqlite:data.db'); session_start(); if (isset($_get['logout'])) {     unset($_session['pass']);     header('location: index.php');     exit(); } if (isset($_session['timeout'])) {     if ($_session['timeout'] + 4 < time()) {         session_destroy();     } }  if (!empty($_post['pass'])) {     $result = $db->query("select user,password users");     foreach ($result $row) {         if (password_verify($_post['pass'], $row['password'])) {             echo "welcome! you're logged in " . $row['user'] . "!  <a href='index.php?logout=true'>logout</a>";             $_session['pass']    = $_post['pass'];             $_session['timeout'] = time();         }     } }  if (empty($_session['pass'])) {     echo '<form method="post" action=""><input type="password" name="pass"><form>'; }  ?> 

my question is: how can use php script protect different files?is there way embed logout link too?

one way store token in session variables when user logs in. confirm token there on each page, if isn't redirect user login page. example assert_login.php:

<?php session_start();   if('' == $_session['token']) {     header("location: login.php");     exit(); } ?> 

then, in php @ top of each of pages:

<?php require('assert_login.php'); ?> 

you can clear session variable on logout, logout.php example:

<?php require('assert_login.php'); // has session_start()  $_session['token'] = ''; // empty token unset($_session['token']); // belt , suspenders header("location: login.php"); exit(); ?> 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -