post - PHP Dynamic signup page

19

I wanted to create a dynamic 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.
  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.
  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.

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?

833

Answer

Solution:

Use isset() to check if $_POST contains data.

http://php.net/isset

129

Answer

Solution:

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

There are a few ways, cookies, storing request ips in a database, 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

but...... The architecture you laid out won't be practical.

Sorry :(

241

Answer

Solution:

To check that request is 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'));
        }
    }

}
?>

People are also looking for solutions to the problem: php - Custom querystring-based routing in CodeIgniter or Phalcon

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.