0

This is my first real attempt at coming to StackExchange and I wanted my question to be as relevant as possible. Basically, I am following along in the O'Reilly book to Learn PHP, MySQL, Javascript, etc. and I'm a bit stuck on Chapter 16's authentication protocol. Basically the author wants us to just put the CSS and JS in the echo <<<_END statements, I'm trying to put it so they can still be kept separate in their external file.

My problem is, when I load the PHP file, absolutely nothing comes up. Not even a title. I was concerned this might have something to do with the fact that I'm running WebMatrix and an IIS Express server for development, but I have been able to run other, much smaller echo <<<_END statements fine when I went to test them.

Below is the code, if anyone has input I would be very grateful.

<?php 

$firstname = $lastname = $username = $password = $age = $email = "";

if (isset($_POST['firstname']))
{
    $firstname = fix_string($_POST['firstname']);
}

if (isset($_POST['lastname']))
{
    $lastname = fix_string($_POST['lastname']);
}

if (isset($_POST['username']))
{
    $username = fix_string($_POST['username']);
}

if (isset($_POST['password']))
{
    $password = fix_string($_POST['password']);
}

if (isset($_POST['age']))
{
    $age = fix_string($_POST['age']);
}

if (isset($_POST['email']))
{
    $email = fix_string($_POST['email']);
}

$fail  = valide_firstname($firstname);
$fail .= valide_lastname($lastname);
$fail .= valide_username($username);
$fail .= valide_password($password);
$fail .= valide_age($age);
$fail .= valide_email($email); 

echo <<<_END
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
<title>An Example Form</title>    
<link rel="stylesheet" type="text/css" href="StyleSheet.css>
</head>
_END;

if ($fail = "")
{
    echo "<body>Form data succesffully validated: $firstname, $lastname, $username, $password, $age, $email.</body></html>";

    //database entry field

    exit;
}

echo <<<_END
<body>
<table class="signup" border="0" cellpadding="2"
cellspacing="5" bgcolor="#eeeeee">
<th colspan="2" align="center">Signup Form</th>

<tr><td colspan="2">Sorry, the following errors were found<br />
in your form: <p><font color=red size=1><i>$fail</i></font></p>
</td></tr>

<form method="post" action = "adduser.php"
onSubmit = "return validate(this)">
<tr><td>First name</td><td><input type ="text" maxlength = "32"
name = "firstname" value = "$firstname" /></td></tr>
<tr><td>Last name</td><td><input type ="text" maxlength = "32"
name = "lastname" value = "$lastname" /></td></tr>
<tr><td>Username</td><td><input type ="text" maxlength = "16"
name = "username" value = "$username" /></td></tr>
<tr><td>Password</td><td><input type ="text" maxlength = "12"
name = "password" value = "$password" /></td></tr>
<tr><td>Age</td><td><input type ="text" maxlength = "3"
name = "age" value = "$age" /></td></tr>
<tr><td>Email</td><td><input type ="text" maxlength = "64"
name = "email" value = "$email" /></td></tr>
<tr><td colspan = "2" align = "center"><input type = "submit" value = "Signup" /></td></tr></form></table>
<script type="text/javascript" src="validate.js"></script>
</body>
</html>
_END;

function validate_fistname($field){
    if ($field == "") return "No First Name was entered <br/>";
}

function validate_lastname($field){
    if ($field == "") return "No Last Name was entered <br/>";
}

function validate_username($field){
    if ($field == "") return "No Username was enterted <br/>";
    else if (strlen($field) < 5) return "Usernames must be at least 5 characters long <br/>";
    else if (preg_match("/[\W]/", $field)) return "Only letters, numbers, - and _ is usernames<br/>";
    return "";
}

function validate_password($field){
    if ($field =="") return "No Password was entered <br/>";
    else if (strlen($field) < 6) return "Passwords must be at least 6 characters in length <br/>";
    else if (!preg_match("/[a-z]/", $field) ||
             !preg_match("/[A-Z]/", $field) ||
             !preg_match("/[\d]/", $field))
             return "Passwords require one upper case, one lower case, and one number<br/>";

    return "";
}

function validate_age($field) {
    if ($field == "") return "No Age was entered<br />";
    else if ($field < 18 || $field > 110)
    return "Age must be between 18 and 110<br />";
    return "";
}

function validate_email($field) {
    if ($field == "") return "No Email was entered<br />";
    else if (!((strpos($field, ".") > 0) &&
        (strpos($field, "@") > 0)) ||
        preg_match("/[^a-zA-Z0-9.@_-]/", $field))
    return "The Email address is invalid<br />";
    return "";
}

function fix_string($string) {
    if (get_magic_quotes_gpc()) $string = stripcslashes ($string);
    return htmlentities ($string);
}
?>
Gavin_Talyn
  • 107
  • 1
  • 2
  • 8

2 Answers2

1

You have syntax errors.
Turn on error reporting as described here.

Error 1
You have spelt validate wrong. This -

$fail  = valide_firstname($firstname);
$fail .= valide_lastname($lastname);
$fail .= valide_username($username);
$fail .= valide_password($password);
$fail .= valide_age($age);
$fail .= valide_email($email); 

must be -

$fail  = validate_firstname($firstname);
$fail .= validate_lastname($lastname);
$fail .= validate_username($username);
$fail .= validate_password($password);
$fail .= validate_age($age);
$fail .= validate_email($email); 


Error 2
Your validate_firstname() function definition is missing an 'r' -

function validate_fistname($field){
                    ^ Needs an 'r' here.

Error 3
The condition if ($fail = "") should be if ($fail == "").

You are presently doing an assignment with = instead of doing a comparison ==

Community
  • 1
  • 1
Kamehameha
  • 5,423
  • 1
  • 23
  • 28
  • That, and `if ($fail = "")` which should be `==` – Funk Forty Niner Mar 23 '14 at 21:11
  • You're welcome. I will **+1** also. Hopefully OP's code will function as intended. – Funk Forty Niner Mar 23 '14 at 21:18
  • 1
    It worked perfectly. Thank you every one who helped. After seeing the errors you all posted out I felt like I had egg all over my face because of the detail portions lol. I guess it's part of learning. But thank you again so much. – Gavin_Talyn Mar 23 '14 at 21:30
  • @Gavin_Talyn Great! Glad to help :) . If this helped, consider [Accepting](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) the answer. Welcome to StackOverflow! – Kamehameha Mar 23 '14 at 21:34
0

You have some PHP errors in it. Let´s begin with your first line. What is $firstname = $lastname = $username = $password = $age = $email = "";?

I can´t see what you try to do with this line. If you want to initialize every variable with "" then write the following:

$firstname = "", $lastname = "", $username = "", $password = "", $age = "", $email = "";

Or much better a single line for every variable:

$fistname = "";
$lastname = "";
$username = "";
$password = "";
$age = "";
$email = "";

You should also not check against $variable == "". This does not make sense in PHP (because you want to check typesafe and this is not typesafe). For typesafe checking in PHP please use 3 equals: $variable === "". Or even better, if you want to check if the string is empty just write empty($variable).

And please use brackets for if statements. This is not a nice and extendable style. Also O´Reilly might not be the best ressource for learning programming. Sure, he got some nice examples for some solutions. But O´Reilly has its own programming style, with that many programmers disagree. And also O´Reilly is out-of-date in most cases.

What you try to use with

echo <<<_END
hello world
_END;

is called heredoc operater, which is nice way to use mutliline strings.

Edit: And it´s also ugly to mix up HTML, JS and PHP. Divide them into different files through an design pattern like MVC or multi tier.

alpham8
  • 1,314
  • 2
  • 14
  • 32
  • You're honestly saying EVERYTHING I've been thinking. The book claims to be one of the best sources out there, but the author (Robin Nixon) says the equivalent of 1 paragraph about calling an external PHP file in the HTML file and then basically says "I don't like doing it, so we're not going over it." I've gone as far to purchase a secondary book, PHP and MySQL Web Development from Pearsons, and I'm hoping that that book has much more in line with correct syntax then I've seen in this book. – Gavin_Talyn Mar 23 '14 at 21:48
  • there are a lot of books about learning the object orientation. I think you should start with one of these. Programming with some basic commands is "simle". But how to handle the object orientation right, is not so simple. Also how to organize your code. – alpham8 Mar 24 '14 at 06:00