简体   繁体   English

登录后如何将用户引导到不同的页面?

[英]How can I direct users to different pages once they've logged in?

I have an online portfolio, where I would like different clients to see different versions of my home page once they have logged in using the credentials I have provided.我有一个在线作品集,我希望不同的客户在使用我提供的凭据登录后看到我主页的不同版本。

So for example:例如:

  • AcmeInc logs in > sees > acmeinc.html AcmeInc 登录 > 看到 > acmeinc.html
  • BobsBurgers logs in > sees > bobsburgers.html BobsBurgers 登录 > 看到 > bobsburgers.html

There won't be many concurrent users (say, never more than 10) I would manually set up each user with a unique id, username, and password.不会有很多并发用户(比如说,永远不会超过 10 个)我会手动为每个用户设置一个唯一的 ID、用户名和密码。 I also have an entry for 'version' where I thought I could assign each user a value, eg 1,2,3 depending on the version of the home page I want them to see - although I am of course unsure if this is a viable approach, or there is something easier / better!我还有一个“版本”条目,我认为我可以为每个用户分配一个值,例如 1,2,3,具体取决于我希望他们看到的主页的版本 - 尽管我当然不确定这是否是可行的方法,或者有更简单/更好的方法!

I have set up a login system using an online PHP tutorial (I don't know much about PHP), and have a mySQL database correctly linked together and it is working as expected when inputting the user credentials into the login form.我已经使用在线 PHP 教程(我对 PHP 不太了解)设置了登录系统,并且将 mySQL 数据库正确链接在一起,并且在将用户凭据输入登录表单时它按预期工作。

The index.html to my site is a login screen, with a form linking to an 'authenticate.php' file (again from this tutorial - code at the bottom of this post)我网站的 index.html 是一个登录屏幕,带有一个链接到“authenticate.php”文件的表单(同样来自本教程 - 这篇文章底部的代码)

On successful login, the user is redirected to home.php, which also has a snippet of PHP at the top to check session status - again attached below.成功登录后,用户被重定向到 home.php,顶部还有一段 PHP 用于检查 session 状态 - 再次附在下面。

Any help would be much appreciated - thank you!任何帮助将不胜感激 - 谢谢!

Authenticate.php验证.php

session_start();
$DATABASE_HOST = 'localhost';
$DATABASE_USER = 'john'
$DATABASE_PASS = 'password';
$DATABASE_NAME = 'users';

$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);
if ( mysqli_connect_errno() ) {
        exit('Failed to connect to MySQL: ' . mysqli_connect_error());
}

if ( !isset($_POST['username'], $_POST['password']) ) {
    exit('Please fill both the username and password fields!');
}

if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {
    $stmt->bind_param('s', $_POST['username']);
    $stmt->execute();
    $stmt->store_result();
    
    if ($stmt->num_rows > 0) {
        $stmt->bind_result($id, $password);
        $stmt->fetch();

        if ($_POST['password'] === $password) {
            session_regenerate_id();
            $_SESSION['loggedin'] = TRUE;
            $_SESSION['name'] = $_POST['username'];
            $_SESSION['id'] = $id;
            header('Location: home.php');
        } else {
            echo 'Incorrect username and/or password!';
        }
    } else {
        echo 'Incorrect username and/or password!';
    }

    $stmt->close();
}

Home.php主页.php

<?php
session_start();
if (!isset($_SESSION['loggedin'])) {
  header('Location: index.html');
    exit;
}
?>

<!DOCTYPE html>
//html follows//

You have already got this in your code.你已经在你的代码中得到了这个。 header('Location: home.php'); this will then direct users to home.php once they successfully login.这将引导用户到 home.php 一旦他们成功登录。

Then on home.php you can add然后在 home.php 你可以添加

session_start();
// If the user is not logged in redirect to the login page...
if (!isset($_SESSION['loggedin'])) {
    header('Location: index.html');
    exit();
}

this will see if user is logged in and if not, redirect user back to login page这将查看用户是否已登录,如果未登录,则将用户重定向回登录页面

Good login page link Not sure if this is what you have been using as code is very similar良好的登录页面链接不确定这是否是您一直在使用的,因为代码非常相似

First of all add function ob_start() before session_start() , see more about ob_start() here from php manual.首先在session_start() ob_start() ) ,请参阅 php 手册中有关ob_start()更多信息。

And as @Luka Shield said, you have already this in your code.正如@Luka Shield 所说,你的代码中已经有了这个。

Answer 2答案 2

if you want redirect user from Authenticate.php , just add this code in your Authenticate.php file after line $_SESSION['id'] = $id;如果您想从Authenticate.php重定向用户,只需将此代码添加到Authenticate.php文件中$_SESSION['id'] = $id;行之后

header('location: user' . $_SESSION['id'] . '.html');

Example & Explanation: if user id is 1 after successful login, will redirect to user1.html and so etc...示例和说明:如果登录成功后用户 id 为 1,将重定向到user1.html等等...

But are you sure that this solution is logical for your project?但是你确定这个解决方案对你的项目是合乎逻辑的吗?

I think it only makes sense to fetch user information on one page based on the identifier (user-id).我认为只有根据标识符(user-id)在一个页面上获取用户信息才有意义。 Better than creating 10 HTML files, ex.比创建 10 个 HTML 文件更好,例如。 (User1.htm, User2-HTML. etc..). (User1.htm、User2-HTML 等。)。 The solution that I will suggest will help you in the future by editing all pages from one place.我将建议的解决方案将通过从一个地方编辑所有页面来帮助您。

Lock at this url example.com/user.php?id=1 .锁定此 url example.com/user.php?id=1

Now I can get the id value by get-request, read here more about $_GET.现在我可以通过 get-request 获取id值,在此处阅读有关 $_GET 的更多信息。

So, first of all let's create user page, create new Page user.php and paste the following code所以,首先让我们创建用户页面,创建新页面user.php并粘贴以下代码

I will give you a page example我会给你一个页面示例

ex: user.php例如:user.php

<?php
    session_start();
    if (!isset($_SESSION['loggedin'])) {
      header('Location: index.html');
        exit;
    }

    if ($_SERVER['REQUEST_METHOD'] == 'GET')
    {
        $id = $_GET['id']; // user id that's coming from url example.com/user.php?id=x
            */

        if ($id > 0 && !empty($id))
        {
            /*
                Now the variable $id represents the value of the number coming from? example.com/user.php?id=x

                Depending on this variable, you can implement your idea, or fetch data depending on the id coming from the link
            */
        }


    }
?>

暂无
暂无

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

相关问题 登录后如何将不同的用户定向到不同的页面? - How to direct different users to different pages after login? PHP:如何阻止对文件的直接URL访问,但仍允许登录用户下载文件? - PHP: How can I block direct URL access to a file, but still allow it to be downloaded by logged in users? 如何根据yii中的登录用户角色设置不同的索引页 - How to set different index pages depending on logged in users role in yii 如何在 header 菜单上为 WP 上的登录和注销用户提供不同的选项? - How can I different options on the header menu for logged in and logged out users on WP? 登录后如何传递用户名 - how can i pass a username once logged in 如何通过1个提交按钮将html / php代码定向到两个不同的页面? - How can I get my html/php code to direct to two different pages from 1 submit button? 如何在 PHP 中根据会话数据将用户重定向到不同的页面?在 PHP 中根据会话数据将用户重定向到不同的页面? - How can I How can I redirect users to different pages based on session data in PHP?redirect users to different pages based on session data in PHP? 用于根据登录角色状态将用户定向到不同页面的按钮 - Button to direct users to different pages based on login role status 如何基于PHP中的会话数据将用户重定向到其他页面? - How can I redirect users to different pages based on session data in PHP? 如何基于会话/表单数据在PHP中有效或无效将用户重定向到其他页面? - How can I redirect users to different pages based on session/form data is valid or not in PHP?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM