如何创建会话登录

时间:2018-01-18 11:36:22

标签: javascript php html mysql session

我正在尝试从html表单创建一个登录会话,该表单将数据发送到mysql数据库,检查凭据并为用户将访问的每个页面启动会话。

问题是我见过

<?php
session_start();
if (!isset($_SESSION["login"]) || $_SESSION["login"] == "")
header("");
else
header("Location:login.php");
?> 

<html>
<body>
...
<\body>
<\html>

但我不喜欢将PHP代码与html一起使用。其实我有一个包含所有php文件的文件夹,有没有办法以这种方式登录?

1 个答案:

答案 0 :(得分:0)

如果您想保护文件,则需要将这些文件保存为php。但是你可以在不同的文件中分离php代码,并将该文件包含在主要php文件的顶部,如require_once(“system_work.php”);在system_work.php中,您可以处理登录和检查会话变量(如果存在或不存在),然后决定是否要退出该页面或让用户留在那里。

我在Codecanyon https://codecanyon.net/item/php-login-user-management-with-message-center/5862673上有登录脚本 我将分享它的登录工作的代码。

请注意,会话无法在html文件中运行,因此如果您想保护文件,该文件需要是HTML。否则无法实现服务器端安全性。但是你可以通过php保存cookie并通过JavaScript访问cookie并通过JavaScript进行验证,但这是相对不安全的方法。

在dashboard.php中,我有以下代码

<?php
include('system_load.php');
//Including this file we load system.
/*
Logout function if called.
*/
if(isset($_GET['logout']) && $_GET['logout'] == 1) { 
    session_destroy();
    HEADER('LOCATION: '.get_option('redirect_on_logout'));
    exit();
} //Logout done.

//user Authentication.
authenticate_user('admin');

$page_title = $label_obj->label('dashboard_title'); //You can edit this to change your page title.
require_once("includes/header.php"); //including header file.

&GT;

此代码后面的主体也在标题之后,然后以同样的方式包含页脚。现在看到system_load.php是包含所有其他类,函数文件和会话信息的文件。我还将与您分享该文件的代码。

system_load.php

<?php
session_start();
/*This file loads system to do basic functions on the site, Please do not change anything here if you dont know what you are doing.*/
include('includes/db_connect.php');
include('includes/functions.php');
//Redirecting to installation wizard if not installed already.
global $db;
//Checks if options exist and installation is complete.
$val = $db->query('SELECT 1 from notes');
if($val == FALSE) {
  HEADER("LOCATION: install.php");
}
include('includes/update.php');
//Session signout after session timeout.
if(isset($_SESSION['timeout'])) {
    if ($_SESSION['timeout'] + get_option('session_timeout') * 60 < time()) {
        session_destroy();
        HEADER('LOCATION: '.get_option('redirect_on_logout'));
        exit();
    }
}
//Adding Language.
include('classes/labels.php');
include('classes/users.php');
include('classes/userlevel.php');
include('classes/notes.php');
include('classes/messages.php');
include('classes/announcements.php');

$label_obj = new WebsiteLabels;
$new_user = new Users;

if(isset($_SESSION['user_id'])):
    $new_user       = new Users;
    $user_status    = $new_user->get_user_info($_SESSION['user_id'], 'status');

    if($user_status == 'ban' || $user_status == 'deactivate' || $user_status == 'suspend') { 
        session_destroy();
        HEADER('LOCATION: index.php');
    }

    $message_obj        = new Messages;
    $new_level          = new Userlevel;
    $notes_obj          = new Notes;
    $announcement_obj   = new Announcements;

    if($new_user->get_user_info($_SESSION['user_id'], 'profile_image') == '') { 
        $profile_img = 'images/thumb.png';
    } else { 
        $profile_img = $new_user->get_user_info($_SESSION['user_id'], 'profile_image');
    }
endif;

让我知道,如果它让你知道如何继续你也可以问我更多的文件代码,如果你需要我可以分享谢谢。