Bipul Roy
Bipul Roy

Reputation: 566

PHP: Proper use of session in a multi-step sign up form

I have made a 3-step(3 different pages) sign up process:

First-step: user provides basic information (name, password, gender, birthday, etc.)

Second-step: user provides extra information ( about, profession, interest, etc.), submit the data and gets a verification link

Third-step: Shows signup status( successful or failed)

I'm using the session to primarily store the information. A session variable like $_SESSION["step_one_complete"] tells me that the previous step was completed which is required. I've used the following code at the beginning of my signup.php page so that every time the signup.php loads the previous session data get cleared:

<?php //signup.php(step one)

session_start();

//Clear previous session data
$_SESSION = array();

//I'm not sure about the two lines bellow
session_destroy();
//Restart session
session_start();

...

Do I need the two lines I have mentioned in the code? Am I using the Session properly? Or there is a better way to handle a multi-step form? Thank you!

Upvotes: 1

Views: 444

Answers (1)

Alex7
Alex7

Reputation: 560

TLDR; No, you don't. You just need to start_session, you don't need to override the session or destroy it.

Maybe i don't understand properly the question, but when you do start_session() you enable the session in your current php script by starting a new session or resuming an existing one. I think that in your case is all you need. Ref: PHP - session_start

Also, i think is bad practice to overwrite the session, you may also delete things you don't intend to. If you want to reset your variables i suggest you store them into an array like

$_SESSION['form']['email']

and then if you want to reset make

$_SESSION['form'] = array();

I think you can do a better job if you make a structure like this:

function is_first_step_completed() // checks if the first step fields are filled or not
function is_second_step_completed() // checks if the second step has all fields completed
if (!is_first_step_completed()) {
// show first step
}

instead of storing a variable is_first_step_completed. If you use a variable like this you would need to make it true or false all the time.

Upvotes: 2

Related Questions