简体   繁体   中英

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. 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? Or should I have another file called Login.php, but then does it redirect back to Front.php if the login was successfull?

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.

you'll need to use php. usually, i create a functions.php file to store all my functions then i include it on the head of each page. 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. 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:

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. So in this case the top of your front.php file would look like:

<?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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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