简体   繁体   English

PHP动态注册页面

[英]PHP Dynamic signup page

I wanted to create a dynamic signup.php. 我想创建一个动态signup.php。 The algorithm is as follow: 算法如下:

Algorithm 算法

  1. when signup.php is requested by client, the code will attempt to check whether user send any data in $_POST. 当客户端请求signup.php时,代码将尝试检查用户是否在$ _POST中发送了任何数据。
  2. if $_POST does not contains any data (means it's the first time user request for signup.php), a signup form will be return to the user, allowing user to enter all his/her details and again send back to signup.php through submit button. 如果$ _POST不包含任何数据(意味着这是用户首次请求signup.php),则将向用户返回一个注册表单,允许用户输入所有他/她的详细信息,然后再次通过以下方式将其发送回signup.php提交按钮。
  3. if $_POST does contains data (means user has fill up the signup form and is now sending all the data back to signup.php), then the php code will attempt validate all those data and return result showing user has been successfully registered or error if failed to do so. 如果$ _POST确实包含数据(意味着用户已经填写了注册表单,现在将所有数据发送回signup.php),则php代码将尝试验证所有这些数据并返回结果,表明用户已成功注册或出错如果失败的话。

The problem I'm having right now is how am I going to check whether it's the first time user request for signup.php or not? 我现在遇到的问题是如何检查用户是否是首次请求signup.php?

Use isset() to check if $_POST contains data. 使用isset()检查$ _POST是否包含数据。

http://php.net/isset http://php.net/isset

To answer your question, "how am I going to check whether it's the first time user request for signup.php or not?", honestly, probably for other users...... 要回答您的问题,“老实说,“我将如何检查这是否是用户首次请求signup.php?”……

There are a few ways, cookies, storing request ips in a database, bleh, bleh, bleh. Cookie有几种方法,可以将请求ips存储在数据库中,bleh,bleh,bleh。 But...... None of them are guaranteed. 但是……他们都不能保证。 The user can disable cookies, use a dynamic ip, etc. You could issue a unique hash and place it as a login.php?q=encValueForUniquePageRequest 用户可以禁用cookie,使用动态ip等。您可以发出唯一的哈希并将其放置为login.php?q = encValueForUniquePageRequest

but...... The architecture you laid out won't be practical. 但是……您布置的体系结构不实用。

Sorry :( 对不起:(

To check that request is POST: 要检查该请求是否为POST:

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){
   //process new user
}
?>

Example: 例:

<?php
Class signup_controller extends controller{
    private $data = array();
    private $model = array();

    function __construct(Core $core){
        parent::__construct($core);

        /* load models - assign to model */
        $this->model['page'] = $this->core->model->load('page_model', $this->core);
        $this->model['auth'] = $this->core->model->load('auth_model', $this->core);

        /* check script is installed - redirect */
        if(empty($this->core->settings->installed)){
            exit(header('Location: '.SITE_URL.'/setup'));
        }
    }

    function index(){
        /* do signup - assign error */
        if($_SERVER['REQUEST_METHOD'] == 'POST'){
            if($this->model['auth']->create_user(1)===false){
                $this->data['error'] = $this->model['auth']->auth->error;
            }
        }

        /* not logged in */
        if(empty($_SESSION['logged_in'])){

            /* assign form keys */
            $_SESSION['csrf']       = sha1(uniqid().(microtime(true)+1));
            $_SESSION['userParam']  = sha1(uniqid().(microtime(true)+2));
            $_SESSION['passParam']  = sha1(uniqid().(microtime(true)+3));
            $_SESSION['emailParam'] = sha1(uniqid().(microtime(true)+4));

            /* get partial views - assign to data */
            $this->data['content_main'] = $this->core->template->loadPartial('partials/signup', null, $this->data);
            $this->data['content_side'] = $this->core->template->loadPartial('about/content_side', null, $this->data);

            /* layout view - assign to template */
            $this->core->template->loadView('layouts/2col', 'content', $this->data);
        }
        /* signed in - redirect */
        else{
            exit(header('Location: ./user'));
        }
    }

}
?>

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

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