帮助初学者使用基于 PHP 的登录系统 [英] Help a Beginner with a PHP based Login System

查看:52
本文介绍了帮助初学者使用基于 PHP 的登录系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点不好意思说,但我在创建基于 PHP 的登录系统时遇到了问题.我正在使用站点模板来处理登录过程的外观,因此我将为您省去代码.这是我关于如何处理登录的思考过程:

I'm a bit embarrassed to say, but I've run into issue with creating a PHP based login system. I'm using a site template to handle the looks of the the login process, so I will spare you the code. Here is my thought process on how to handle the login:

创建一个简单的 login.php 文件.将有一个表单,其动作设置为自身.它将检查提交是否已被单击,如果是,则验证以确保用户输入了有效的密码/用户名.如果他们这样做,设置一个会话变量保存一些登录信息(用户名,不是密码),并将它们重定向到一个受限区域.如果登录信息无效,则将错误消息保存在会话变量中,显示错误消息以提供进一步说明,并等待用户重新提交.这是我所拥有的一部分 - 希望你们中的一位专家可以看到我哪里出错了,并给我一些见解:

Create a simple login.php file. On there will be a form whose action is set to itself. It will check to see if the submit has been clicked, and if so validate to make sure the user entered a valid password / username. If they do, set a session variable save some login info (username, NOT password), and redirect them to a restricted area. If the login info isn't valid, save an error message in a session variable, display error message giving further instruction, and wait for the user to resubmit. Here is a chunk of what I have - hopefully one of you experts can see where I've gone wrong, and give me some insight:

<?php
session_start();

if(isset($_POST['submit'])) {
    if(!empty($_POST['username']) AND !empty(!$_POST['password']))
    {
        header("Location: http://www.google.com");
    } else {
        $err = 'All the fields must be filled in!';
    }
}

if($err) {
    $_SESSION['msg']['login-err'] = $err;
}
?>

现在上面只是一个例子 - 上面代码的目的是处理用户输入,脚本只是验证用户是否输入了用户名和密码.如果他们有,我希望他们,在这种情况下,被重定向到 google.com(为了这个例子).如果没有,请保存错误消息.鉴于我当前的代码,错误消息将完美显示,但是如果用户提交并输入了用户名和密码的内容,则页面根本不会重定向.我确定这是一个愚蠢的问题,但我是一个初学者,老实说,现在有点嗡嗡声.非常感谢!

Now the above is just an example - the intent of the above code is to process user input, with the script validating simply that the user has given input for username and password. If they have, I would like them, in this case, to be redirected to google.com (for the sake of this example). If not, save an error message. Given my current code, the error message will display perfectly, however if the user submits and has something entered for the username and password, the page simply doesn't redirect. I'm sure this is a silly question, but I am a beginner, and well, to be honest, a bit buzzed right now. Thanks so much!

推荐答案

好吧,如果不查看其余代码,很难说清楚.但是对于标头,标头必须在任何内容写入响应流之前发送(即在任何 echo 语句或任何未包含在 <?php ... 中的代码之前)?> 标签),所以这可能是它不重定向的原因.你提供的代码我觉得不错.

Well, without looking at the rest of your code it's difficult to say. But with headers, the header must be sent before anything is written to the response stream (ie before any echo statements OR any code that is not included inside of <?php ... ?> tags) so that could be why it isn't redirecting. The code you supplied looks good to me.

这篇关于帮助初学者使用基于 PHP 的登录系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆