简体   繁体   English

用户登录和管理集成到单页站点中

[英]User login and management integration into single page site

So I finally finished designed and building my website, it is a single page (Front.php) that displays all the menu options including register where the user can register and then the information gets stored into a database. 因此,我终于完成了网站的设计和构建,它是一个页面(Front.php),其中显示了所有菜单选项,包括用户可以在其中注册的注册选项,然后将信息存储到数据库中。 The thing I need to understand and implement now is the idea of a user account. 我现在需要了解和实现的是用户帐户的概念。 I'm not understanding how I should go about having login and account management. 我不了解如何进行登录和帐户管理。 I understand that they can login, and i can just search the database for their username, and if there's a match then they can login. 我知道他们可以登录,并且我可以在数据库中搜索他们的用户名,如果有匹配项,他们就可以登录。 But where should that be handled, Front.php? 但是Front.php应该在哪里处理? Or should I have another file called Login.php, but then does it redirect back to Front.php if the login was successfull? 还是我应该有另一个名为Login.php的文件,但是如果登录成功,它会重定向回Front.php吗?

I'm really kind of lost on the basic file structure. 我真的迷失在基本文件结构上。 Can someone explain how I should go about this? 有人可以解释我应该怎么做吗?

My page is 95% jquery/html, with php only doing the inserting into the database function. 我的页面是95%jquery / html,其中php仅执行插入数据库功能。

you'll need to use php. 您将需要使用php。 usually, i create a functions.php file to store all my functions then i include it on the head of each page. 通常,我创建一个functions.php文件来存储我的所有函数,然后将其包含在每页的开头。 in my functions file, I always put a function to check if the login exists. 在我的函数文件中,我总是放置一个函数来检查登录名是否存在。 here it is verbatim: 这里是逐字记录:

function checkLogin($data) {
    global $con;
    extract($data);
    $sql = "SELECT * FROM users WHERE username='$username' AND password='$password'";
    $result = mysql_query($sql, $con) or exit('Eror selecting users database: '.mysql_error());
    $num_rows = mysql_num_rows($result);
    if ($num_rows==0) {
        return false;
    }
    else {
        return true;
    }
}

$con is a global variable I store which creates the MySQL connection. $con是我存储的全局变量,用于创建MySQL连接。 It looks like: 看起来像:

$con = mysql_connect($host, $user, $pass);

This way I only have to define it once. 这样,我只需要定义一次。 Then on front.php you submit the form to itself (front.php) and do something like: 然后在front.php上将表单提交给自己(front.php)并执行以下操作:

if (isset($_POST['submit']) && isset($_POST['loginForm'])) {
    if (!checkLogin($_POST)) {
        $valid = false;
    }
    else {
        Header('Location: dashboard.php');
    }
}

I put this code in a file called session.php, and include it at the head of every page. 我将此代码放在一个名为session.php的文件中,并将其包含在每个页面的开头。 So in this case the top of your front.php file would look like: 因此,在这种情况下,front.php文件的顶部将如下所示:

<?php
  include('session.php');include('functions.php');

The reason I keep them separate is because you don't need to check for the login on every page the functions are in. in this example, I have my login form action set to loginForm and if successful, I am redirecting them to dashboard.php. 之所以将它们分开,是因为您不需要在函数所在的每个页面上都检查登录名。在此示例中,我将登录表单操作设置为loginForm ,如果成功,则将它们重定向到仪表板。的PHP。 You should probably put something in that else statement that stores their session as a cookie so they dont have to keep logging back in every time they close the tab or exit the browser. 您可能应该在else语句中放入一些内容,将其会话存储为cookie,以便他们不必在每次关闭选项卡或退出浏览器时都保持登录状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM