简体   繁体   中英

Registration Form (HTML, PHP & MySQL) redirect after error or missing values

I am new to development with HTML, PHP, CSS etc.

I need to do this small Registration and Login Form. I haven't gone into detail in Object-Oriented PHP and I'm working in the simplest manner as this task needs to be done in a short time and I've only been coding and experimenting with these languages these past two days.

What I already have is a working Registration and Login forms that when they are submitted the information is posted to another php file. Than it verifies that the data has been entered and that the e-mail isn't already used. What i need is that when either the e-mail is already used and when a field is left empty that it automatically goes back to Registration/Login forms and displays a message with their respective errors.

I know this might have been done in a different way but the deadline is really close and so i need a solution that works with what I already have.

I'm sorry if a similar question is already available, but i might be using incorrect keywords to search for the solution.

Keywords currently that I'm using are Redirection, "Going back to previous page".

If JavaScript is more suitable for this kind of operation it is also excepted and would like this to be pointed out, although a php solution would be appreciated a bit more as i currently obtain more knowledge on PHP than on JavaScript.

Thanks, any help or directions to suitable solutions would be mostly appreciated.

There are many different ways to do this. If when the form is submitted your executing the code on a different php file then you could have an IF statement there which redirects the headers back to the form page if there are errors with the users input such as:

if($username == "")
{
    header("Location: YOUR_FORM_PAGE.php");
}

Hope this helps.

To add the execution to the same page you can do this.

add these buttons;

<input type="hidden" id="submitted" name="submitted" value="1">
<span class="label"></span><input type="submit" class="submit"  value="Submit"><input type="reset" class="submit"  value="Clear">

set the form action to the form page. Add this to the top of the page and add any PHP you want to it;

if(isset($_POST["submitted"]) && $_POST["submitted"] == 1)
{

for example;

if($from_fullname == "")
    {
        $submission_status = '<div class="vpb_info" align="left">Please enter your fullname in the required field to proceed. Thanks.</div>';
    }

and add this to the page where you want the error displayed;

 <p> <?php echo $submission_status; ?></p>

You can use

  • A) PHP header() function
  • B) echo a META HTTP - EQUIV=" refresh " CONTENT=" seconds; URL = \\the-other-url">
  • C) Use JS like this but you will need to set a timeout

Example:

if (empty ($_POST ['username']) || empty ($_POST ['password']))
{
    echo "Please enter a username, or password";
    header ("refresh:5; url=back.php");
    exit;
}

Sounds like http-redirect ( http://php.net/manual/en/function.http-redirect.php ) should help you out. You can redirect on your error conditions with that.

Update

With redirect you can attatch QueryString Parameters so you could redirect back to the login.php page with an errror code and/or message as a query string perameter.

These will get you started, a registration or signup php file and a login php file I made once (personal info is faked).

SignUp.php

<?php 

session_start();

$name = $_REQUEST['name'] ;
$userpassword = hash('sha512',$_REQUEST['password'] );
$signature = $_REQUEST['signature'] ;
$image = $_REQUEST['image'];
$email = $_REQUEST['email'] ;
$emailreplies = $_REQUEST['emailreplies'] ;




if (!isset($_REQUEST['name'])) {
    header( "Location: MotesBlog.php" );
}else{

$username="root";
$password="root";
$database="MotesBlog";

mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

    $query=sprintf("SELECT Name FROM users WHERE Name LIKE '%s';",
                   mysql_real_escape_string($name));
    $query=mysql_query($query);

    if(mysql_num_rows($query)){

        $query = sprintf("SELECT Email FROM users WHERE Name='%s';",
                                 mysql_real_escape_string($name));
        $query=mysql_query($query);

        $_SESSION['NameTaken'] = true;
        $_SESSION['UsedName'] = $name;
        $_SESSION['UsedEmail'] = mysql_result($query,0);
        header("Location: SignUp.html");
    }else{

    $query=sprintf("SELECT Email FROM users WHERE Email LIKE '%s';",
                   mysql_real_escape_string($email));
    $query=mysql_query($query);

    if(mysql_num_rows($query)){

        $_SESSION['EmailTaken'] = true;
        $_SESSION['UsedEmail'] = $email;
        header("Location: SignUp.html");
    }else{

        $query = sprintf(" INSERT INTO users VALUES (
        NULL , '%s', '%s' , '%s', '%s', '%s', CURRENT_TIMESTAMP , 0, $emailreplies);",
        mysql_real_escape_string($name),
        $userpassword,
        mysql_real_escape_string($signature),
        mysql_real_escape_string($image),
        mysql_real_escape_string($email));

        mysql_query($query);


        $query = sprintf("SELECT JoinDate FROM users WHERE Name='%s';",
                         mysql_real_escape_string($name));

        $vcode=md5(mysql_result(mysql_query($query),0));

        mysql_close();


        require_once "Mail.php";

        $from = "PocketWoods Hunting Hall<pwoods@email.com>";
        $to = $email;
        $subject = "Welcome to Motes Blog";
        $body = "<html>
                 <body>Thank you for your time. <br/>
                 To ensure a human made this account and not an 
                 automated process please click the link below:<br>
                 <a href=\"http://site.com/MotesBlog/verifyaccount.php?vcode=".$vcode."&name=".$name."\">
                 Activate Account
                 </a>
                 </body>
                 </html>";  

        $host = "mail.root.com";
        $username = "root@root.com";
        $password = "root";

        $headers = array (  'From' => $from,
                            'To' => $to,
                            'Subject' => $subject,
                            'MIME-Version' => "1.0",
                            'Content-type' => "text/html; charset=iso-8859-1");
        $smtp = Mail::factory('smtp',
                array ( 'host' => $host,
                        'auth' => false,
                        'username' => $username,
                        'password' => $password));

        $mail = $smtp->send($to, $headers, $body);


        header("Location: success.html");
        }
    }
}
?>

Login.php

<?php

session_start();



$username="root";
$password="root";
$database="MotesBlog";

mysql_connect("localhost",$username,$password);
@mysql_select_db($database) or die( "Unable to select database");

$user_name = $_REQUEST['name'];
$user_password = $_REQUEST['password'];

if (!isset($_REQUEST['name'])) {
    header( "Location: MotesBlog.php" );
}else{

    if(isset($_SESSION['User'])){
        if( ($_SESSION['CreatedTime'] + 3600) < time() ){

            $_SESSION['Expired'] = true;
        }
        unset($_SESSION['User']);
    }




    $query = sprintf("SELECT Password FROM users WHERE Name='%s';",
    mysql_real_escape_string($user_name));
    $query=mysql_query($query);

    if(mysql_num_rows($query)){
        $real_password=mysql_result($query,0);

        $query = sprintf("SELECT Email FROM users WHERE Name='%s';",
        mysql_real_escape_string($user_name));

        $query=mysql_query($query);
        $email = mysql_result($query,0);

        if($real_password == hash('sha512',$user_password)){



            $query = sprintf("SELECT Validated FROM users WHERE Name='%s';",
            mysql_real_escape_string($user_name));
            $query=mysql_query($query);

            mysql_close();

                if(mysql_result($query,0)){

                    $_SESSION['User'] = $user_name;
                    $_SESSION['CreatedTime'] = time();
                    setcookie("User", $_REQUEST['name'], time() + 60*60*24*365);

                    header( "Location: MotesBlog.php" );

                }else{

                    $_SESSION['resend_name'] = $user_name;
                    $_SESSION['resend_email'] = $email;
                    $_SESSION['NotValidated'] = true;

                    header( "Location: MotesBlog.php" );

                    }
            }else{

                $_SESSION['WrongPW'] = true;
                $_SESSION['UsedEmail'] = $email;
                header( "Location: MotesBlog.php" );
            }
        }else{
            $_SESSION['WrongName'] = true;
            header( "Location: MotesBlog.php" );
        }


}

?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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