0

Error messages should be displayed within the html if certain fields on my member register form are left empty, if they are invalid types or if the username entered already exists. I've tried adding a record with an already existing username and it does direct to the member registration failed webpage however the error messages aren't displayed.

I have shown the code for the member_registration file, validation file and the member_registration_failed file below:

member_registration.php:

<?php

    require_once('InitDB.php');
    
    echo <<< HTML
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Member Login</title>
    <link rel="stylesheet" type="text/css" media="screen" href="registration.css" />
    </head>
    <body>
        <div id = "whole">
            <div id = "mainHeading">
                <h1>Taxi Service</h1>
            </div>
            <div id = "mainLinks">
                    <a class="BookTaxiLink" href="">Book a Taxi Journey</a>
            </div>
                <div id = "contentLinks">
                        <a class="indContentLinks" href="">Link 1</a>
                        <a class="indContentLinks" href="">Link 2</a>
                        <a class="indContentLinks" href="">Link 3</a>
                </div>
                <div id = "content">
                    <p class = "instructions">
                        Please enter your details in the fields below to register...
                    </p>
                    <form method="post" action="validate_member_registration.php">
                        <table>
                            <tr>
                                <td class = "labels">
                                    First Name:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="forename" size="30" maxlength="70" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Surname:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="surname" size="30" maxlength="70" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Title:
                                </td>
                                <td class = "fields">
                                    <select name="title" width="30">
                                        <option value="" selected="selected">Please choose...</option>
                                        <option value="Mr">Mr</option>
                                        <option value="Master">Master</option>
                                        <option value="Mrs">Mrs</option>
                                        <option value="Miss">Miss</option>
                                        <option value="Ms">Ms</option>
                                        <option value="Dr">Dr</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Date of Birth:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="dob" size="30" maxlength="10" />
                                </td>
                                <td class = "rules">
                                    Format: YYYY-MM-DD
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Username:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="username" size="30" maxlength="30" />
                                </td>
                                <td class = "rules">
                                    Alphanumeric characters only
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Password:
                                </td>
                                <td class = "fields">
                                    <input type="password" name="password" value="" size="30" maxlength="30"/>
                                </td>
                                <td class = "rules">
                                    Alphanumeric characters only
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    House Number:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="houseNo" size="30" maxlength="30" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Street:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="street" size="30" maxlength="100" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Area:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="area" size="30" maxlength="100" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Town/City:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="town" size="30" maxlength="100" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Postcode:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="postcode" size="30" maxlength="15" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Telephone Number:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="telNo" size="30" maxlength="15" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Email Address:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="email" size="30" maxlength="150" />
                                </td>
                            </tr>
                        </table>
                        
                        <p class = "buttons">
                        <input type="submit" value="Submit" />
                        <input type="reset" value="Clear" />
                        </p>
                    </form>
                    <p class = "instructions">
                        If you already have an account, click
                        <a class="loginLink" href="login.php">here</a>.
                    </p>
                </div>
            <div id = "footer">
                <p> This is the footer </p>
            </div>
        </div>
    </body>
    </html>
HTML;
 
?>

validate_member_registration.php:

<?php
    session_start();
    include'db_config.inc';
    
    $error_flag = false;
    $_SESSION['ERR_MSG1'] = '';
    $_SESSION['ERR_MSG2'] = '';
    $_SESSION['ERR_MSG3'] = '';
    $_SESSION['ERR_MSG4'] = '';
    $_SESSION['ERR_MSG5'] = '';
    $_SESSION['OVR_ERR_MSG'] = '';
    
    try {
        $dbh = new PDO("mysql:host=127.0.0.1;dbname=$db", $user, $password);
    }
    catch (PDOException $e) {
        die($e->getMessage());
    }
    
    if(!isset($_POST['username'])) {
        $error_flag = true;
        $_SESSION['ERR_MSG1'] = 'Username field empty!';
    }
    
    if(!isset($_POST['password'])) {
        $error_flag = true;
        $_SESSION['ERR_MSG2'] = 'Password field empty!';
    }
    
    if(!isset($_POST['houseNo'])) {
        $error_flag = true;
        $_SESSION['ERR_MSG3'] = 'House Number field empty!';
    }
    
    if(!isset($_POST['postcode'])) {
        $error_flag = true;
        $_SESSION['ERR_MSG4'] = 'Postcode field empty!';
    }
    
    if(!isset($_POST['email'])) {
        $error_flag = true;
        $_SESSION['ERR_MSG5'] = 'Email field empty!';
    }
    
    $username = $_POST['username'];
    $membermatches = $dbh->query("SELECT username FROM Member WHERE username = '$username'");
    
    if($error_flag){
        $_SESSION['OVR_ERR_MSG'] = 'Please enter your details again making sure to fill in the empty fields!';
        header("location: member_registration_failed.php");
    }
    else if($membermatches->rowCount() == 1) {
        $_SESSION['OVR_ERR_MSG'] = 'Username already exists. Please enter a different username.';
        header("location: member_registration_failed.php");
    }
    else {
            try{
            $forename = filter_var($_POST['forename'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
            $surname = filter_var($_POST['surname'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
            $title = $_POST['title'];
            $dob = $_POST['dob'];
            $username = filter_var($_POST['username'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
            $password = filter_var($_POST['password'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
            $houseno = filter_var($_POST['houseNo'], FILTER_SANITIZE_NUMBER_INT);
            $street = filter_var($_POST['street'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
            $area = filter_var($_POST['area'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
            $town_or_city = filter_var($_POST['town'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
            $postcode = filter_var($_POST['postcode'], FILTER_SANITIZE_STRING, FILTER_FLAG_NO_ENCODE_QUOTES);
            $telno = filter_var($_POST['telNo'], FILTER_SANITIZE_NUMBER_INT);
            $email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
            
            $dbh->exec("INSERT INTO `Member` 
			   (`username`,`createDate`,`password`,`siteRole`,`surname`,`forename`,`title`,`dob`,
			   `houseNo`,`street`,`area`,`town_or_city`,`postcode`,`telNo`,`email`)
			   VALUES ('$username', CURDATE(), '$password', 'MEMBER', '$surname', '$forename', '$title', '$dob',
			   '$houseno', '$street', '$area', '$town_or_city', '$postcode', '$telno',
			   '$email'
			   )");
            
            $_SESSION['SESS_USER'] = $username;
            header("location: member_profile.php");
        }
        catch(Exception $e){
            $_SESSION['OVR_ERR_MSG'] = $e;
            header("location: member_registration_failed.php");
        }
    }
?>

member_registration_failed.php:

<?php

    require_once('InitDB.php');
    session_start();
    
    $errmsg1 = $_SESSION['ERR_MSG1'];
    $errmsg2 = $_SESSION['ERR_MSG2'];
    $errmsg3 = $_SESSION['ERR_MSG3'];
    $errmsg4 = $_SESSION['ERR_MSG4'];
    $errmsg5 = $_SESSION['ERR_MSG5'];
    $ovrerrmsg = $_SESSION['OVR_ERR_MSG'];
    
    echo <<< HTML
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Member Login</title>
    <link rel="stylesheet" type="text/css" media="screen" href="registration.css" />
    </head>
    <body>
        <div id = "whole">
            <div id = "mainHeading">
                <h1>Taxi Service</h1>
            </div>
            <div id = "mainLinks">
                    <a class="BookTaxiLink" href="">Book a Taxi Journey</a>
            </div>
                <div id = "contentLinks">
                        <a class="indContentLinks" href="">Link 1</a>
                        <a class="indContentLinks" href="">Link 2</a>
                        <a class="indContentLinks" href="">Link 3</a>
                </div>
                <div id = "content">
                    <p class = "instructions">
                        Please enter your details in the fields below to register...
                    </p>
                    <form method="post" action="validate_member_registration.php">
                        <table>
                            <tr>
                                <td class = "labels">
                                    First Name:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="forename" size="30" maxlength="70" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Surname:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="surname" size="30" maxlength="70" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Title:
                                </td>
                                <td class = "fields">
                                    <select name="title" width="30">
                                        <option value="" selected="selected">Please choose...</option>
                                        <option value="Mr">Mr</option>
                                        <option value="Master">Master</option>
                                        <option value="Mrs">Mrs</option>
                                        <option value="Miss">Miss</option>
                                        <option value="Ms">Ms</option>
                                        <option value="Dr">Dr</option>
                                    </select>
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Date of Birth:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="dob" size="30" maxlength="10" />
                                </td>
                                <td class = "rules">
                                    Format: YYYY-MM-DD
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Username:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="username" size="30" maxlength="30" />
                                </td>
                                <td class = "rules">
                                    <?php echo $errmsg1; ?>
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Password:
                                </td>
                                <td class = "fields">
                                    <input type="password" name="password" value="" size="30" maxlength="30"/>
                                </td>
                                <td class = "rules">
                                    <?php echo $errmsg2; ?>
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    House Number:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="houseNo" size="30" maxlength="30" />
                                </td>
                                <td class = "rules">
                                    <?php echo $errmsg3; ?>
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Street:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="street" size="30" maxlength="100" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Area:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="area" size="30" maxlength="100" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Town/City:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="town" size="30" maxlength="100" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Postcode:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="postcode" size="30" maxlength="15" />
                                </td>
                                <td class = "rules">
                                    <?php echo $errmsg4; ?>
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Telephone Number:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="telNo" size="30" maxlength="15" />
                                </td>
                            </tr>
                            <tr>
                                <td class = "labels">
                                    Email Address:
                                </td>
                                <td class = "fields">
                                    <input type="text" name="email" size="30" maxlength="150" />
                                </td>
                                <td class = "rules">
                                    <?php echo $errmsg5; ?>
                                </td>
                            </tr>
                        </table>
                        
                        <p class = "buttons">
                        <input type="submit" value="Submit" />
                        <input type="reset" value="Clear" />
                        </p>
                    </form>
                    <p class = "instructions">
                        <?php echo $ovrerrmsg; ?>
                        
                        If you already have an account, click
                        <a class="loginLink" href="login.php">here</a>.
                    </p>
                </div>
            <div id = "footer">
                <p> This is the footer </p>
            </div>
        </div>
    </body>
    </html>
HTML;
 
?>

I receive no errors for the code however there are no error messages from the sessions outputted on failure.

Any help is appreciated, thanks.

3
Contributors
14
Replies
15
Views
5 Years
Discussion Span
Last Post by BenzZz
Featured Replies
  • I see what you are doing now and there is no reason or need to have a whole html page echoed in php so change (plus you are correct you can't use <?php ?> within other php tags but the way you are doing things you also need to end … Read More

0

On last file try to move session_start(); at very top of the file. Bye.

Tryed it. Still no change.

0

Firstly, there is no need to re-assign your session variables to normal variables, just use them as they are. Secondly, and more importantly, I cannot see anywhere on the page that actually echos the error messages which is why you won't see them.

0

Firstly, there is no need to re-assign your session variables to normal variables, just use them as they are. Secondly, and more importantly, I cannot see anywhere on the page that actually echos the error messages which is why you won't see them.

The echos are on lines 91, 102, 113, 148, 167 and 178.

I did originally just try to echo the session variables but it said something about type or whitespace which is why i assigned them to variables.

0

Sorry, I can see them now. Have you tried using var_dump($_SESSION) at the top of the page (after session_start()) to ensure that your session data is there?

0

Sorry, I can see them now. Have you tried using var_dump($_SESSION) at the top of the page (after session_start()) to ensure that your session data is there?

I added var_dump($_SESSION) and attempted to register an already existing username.

The result was this:

array
'ERR_MSG1' => string '' (length=0)
'ERR_MSG2' => string '' (length=0)
'ERR_MSG3' => string '' (length=0)
'ERR_MSG4' => string '' (length=0)
'ERR_MSG5' => string '' (length=0)
'OVR_ERR_MSG' => string 'Username already exists. Please enter a different username.' (length=59)

It shows the session variables are working and passing so is it a problem with the echos?

0

So it seems, I've tried this and hello is not printed:

<?php
echo <<<HTML
test 1
<?php echo 'hello'; ?>
test 2
HTML;
?>

Edited by cereal: n/a

0

So it seems, I've tried this and hello is not printed:

<?php
echo <<<HTML
test 1
<?php echo 'hello'; ?>
test 2
HTML;
?>

Yeah, i've just took the OVR_ERR_MSG out of it's own php echo tags and the message is now displayed so it seems that was the problem.

However i now have a new problem. As stated earlier, i've changed my file to try and print the session variables without assigning them to variables e.g.

from this:

<?php
    session_start();
    var_dump($_SESSION);
    require_once('InitDB.php');
    
    [B]$errmsg1 = $_SESSION['ERR_MSG1'];
    $errmsg2 = $_SESSION['ERR_MSG2'];
    $errmsg3 = $_SESSION['ERR_MSG3'];
    $errmsg4 = $_SESSION['ERR_MSG4'];
    $errmsg5 = $_SESSION['ERR_MSG5'];
    $ovrerrmsg = $_SESSION['OVR_ERR_MSG'];[/B]
    
    echo <<< HTML
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Member Login</title>
    <link rel="stylesheet" type="text/css" media="screen" href="registration.css" />
    </head>
    <body>
        <div id = "whole">
            <div id = "mainHeading">
                <h1>Taxi Service</h1>
            </div>
            <div id = "mainLinks">
                    <a class="BookTaxiLink" href="">Book a Taxi Journey</a>
            </div>
                <div id = "contentLinks">
                        <a class="indContentLinks" href="">Link 1</a>
                        <a class="indContentLinks" href="">Link 2</a>
                        <a class="indContentLinks" href="">Link 3</a>
                </div>
                <div id = "content">
                    <p class = "instructions">
                        Please enter your details in the fields below to register...
                    </p>
                    [B]<p class = "instructions">
                        $ovrerrmsg
                    </p>[/B]

To this:

<?php
    session_start();
    var_dump($_SESSION);
    require_once('InitDB.php');
    
    echo <<< HTML
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Member Login</title>
    <link rel="stylesheet" type="text/css" media="screen" href="registration.css" />
    </head>
    <body>
        <div id = "whole">
            <div id = "mainHeading">
                <h1>Taxi Service</h1>
            </div>
            <div id = "mainLinks">
                    <a class="BookTaxiLink" href="">Book a Taxi Journey</a>
            </div>
                <div id = "contentLinks">
                        <a class="indContentLinks" href="">Link 1</a>
                        <a class="indContentLinks" href="">Link 2</a>
                        <a class="indContentLinks" href="">Link 3</a>
                </div>
                <div id = "content">
                    <p class = "instructions">
                        Please enter your details in the fields below to register...
                    </p>
                    [B]<p class = "instructions">
                        $_SESSION['OVR_ERR_MSG']
                    </p>[/B]

Since i have tried to print the session variable string directly (highlighted in bold just above) i receive the following error:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\EasyPHP-5.3.8.1\www\Final Year Project\member_registration_failed.php on line 39

Anyone know how i can change this?

0

You shouldn't just be echoing out the errors without checking if they have any data first. For example, change this

<?php echo $errmsg1; ?>

To this:

<?php if (isset($_SESSION['ERR_MSG1']) && !empty($_SESSION['ERR_MSG1'])) {
echo $_SESSION['ERR_MSG1'];
}

And see what happens (you will obviously need to change them all or the one that you know will have data to test.

Edited by simplypixie: Posted at the same time as your post above

0

Sorry posted my first reply at the same time as you posted again so may not be relevant now.

This won't work though

<p class = "instructions">
                        $_SESSION['OVR_ERR_MSG']
                    </p>

You have no PHP tags, should be

<p class = "instructions">
                        <?php echo $_SESSION['OVR_ERR_MSG']; ?>
                    </p>

Edited by simplypixie: n/a

0

The messages weren't even displaying though before when i had <?php ?> tags within the overall <?php ?> tags. Is this valid?

0

Sorry posted my first reply at the same time as you posted again so may not be relevant now.

This won't work though

<p class = "instructions">
                        $_SESSION['OVR_ERR_MSG']
                    </p>

You have no PHP tags, should be

<p class = "instructions">
                        <?php echo $_SESSION['OVR_ERR_MSG']; ?>
                    </p>

I've just tried this and i still get the following error relating to echoing $_SESSION:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\Program Files\EasyPHP-5.3.8.1\www\Final Year Project\member_registration_failed.php on line 39
2

I see what you are doing now and there is no reason or need to have a whole html page echoed in php so change (plus you are correct you can't use <?php ?> within other php tags but the way you are doing things you also need to end and restart <<< HTML each time you want to echo php.
Change both your registration and registration failed pages.
Registration:

<?php

    require_once('InitDB.php');
?>    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Member Login</title>
    <link rel="stylesheet" type="text/css" media="screen" href="registration.css" />
    </head>
    <body>
.
.
.
.
.
.
 </body>
    </html>

Fail page:

<?php

    require_once('InitDB.php');
    session_start();
    
    $errmsg1 = $_SESSION['ERR_MSG1'];
    $errmsg2 = $_SESSION['ERR_MSG2'];
    $errmsg3 = $_SESSION['ERR_MSG3'];
    $errmsg4 = $_SESSION['ERR_MSG4'];
    $errmsg5 = $_SESSION['ERR_MSG5'];
    $ovrerrmsg = $_SESSION['OVR_ERR_MSG'];
 ?>   
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Member Login</title>
    <link rel="stylesheet" type="text/css" media="screen" href="registration.css" />
    </head>
    <body>
.
.
.
.
.
    </body>
    </html>
Votes + Comments
Well explained!
0

Thanks for all of the help! That works.

I'll add in the checks to see if the session variables are empty too.

Regarding the principle of echoing all of the html, i didn't know whether it was good practice or not.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.