Validando formulario usando sesión php

I am trying to validate my form using php. Upon validation it will give a welcome message.Or else it will redirect to the index page. I have used session to save the variable. But the problem is nothing happen's when I submit the form, Here's my script

<?php session_start();
    $_SESSION['reg'] = array();
    $name = $_POST['name'];
    $email = $_POST['email'];
    $passwd = $_POST['passwd'];
    $repasswd = $_POST['repasswd'];

if(empty($_POST)){
    header("location:register.php");
}
else
{
    if(empty($name)){
        $_SESSION['reg']['name'] = "Please enter name";
    }
    if(empty($email)){
        $_SESSION['reg']['email'] = "Please enter email";
    }
    if (empty($passwd)) {
        $_SESSION['reg']['passwd'] = "please enter a password";
    }
    elseif (strlen(passwd)>16) {
        $_SESSION['reg']['passwd'] = "At most 16 chars";
    # code...
    }
    if ($passwd != $repasswd) {
        $_SESSION['reg']['repasswd'] = "Passwords don't match";
    }
    if (empty($_SESSION['reg'])) {
        header("location:welcome.php");
    }
    else
    {
        $_SESSION['data'] = array();
        foreach($_POST as $id=>$val)
        {
            $_SESSION['data'][$id] = $val;

        }
        header("location:register.php");
    }

?>      

when I submit the form, It shows a blank page.

preguntado el 27 de noviembre de 13 a las 05:11

¿Qué hay de la configuración ini_set('display_errors','on') at the top of your page and then try -

Doesn't do anything :( .. sould I place this before <?php ? -

no, it should be after the <?php the second thing i'd suggest checking the php logs, i don't know why but sometimes I've encountered a similar scenario, blank page, no errors, it still gets logged though, so check there. -

Thanks.. I found the error.. It was a bracket.. Nice suggestion , I am a beginner, silly mistake. -

1 Respuestas

The problem is that you are not storing your post variables into the session if they are NOT empty prior to checking

if (empty($_SESSION['reg'])) {

I would highly suggest doing some sql injection prevention prior to putting post variables into the session scope.

UPDATE: So if you want the variable in the session scope, instead of checking if(empty($name)){ and setting an error message in the session, I would do this:

if( !empty($name) ){
   $_SESSION['reg']['name'] = $name;
} else {
   // error handling
}

this will set the session variable if the posted value is NOT empty. Now when you check your session variable later on, it will have the name value in it and won't be empty.

respondido 27 nov., 13:07

I would do sql injection prevention later, but I don't understand what you are telling me to do.. - Tamim Addari

If (empty($_SESSION['reg'])) { is true, I shouldn't store the post variables, It should just go to the welcome page. - Tamim Addari

I updated my answer with an example of how to set the session variable. - Revocar

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.