
Originally Posted by
dog
Even when using the Location header - which usually generates a 302 Found response - a short HTML message should be included.
Ok, I'll put one in if you can tell me how.
Perhaps the easiest approach in the short term is to drop out of PHP parsing mode after sending the header, output the HTML, then re-enter PHP mode and call the exit function:
PHP Code:
$host = $_SERVER['HTTP_HOST'];
$path = rtrim(dirname($_SERVER['PHP_SELF']), '/') . '/form.php';
$uri = 'http://' . $host . $path;
header('Location: ' . $uri);
/* Drop out of PHP */
?>
<!DOCTYPE ...>
<!-- ... -->
<a href="<?php echo $uri ?>">...</a>
<!-- ... -->
<?php
exit();
Parts have clearly been omitted, but after filling out the HTML part in the middle, you should be able to drop this in place of the code you posted previously that set the Location header.
I'm testing their existance manually, I think.
The isset function tests for existence, though some array functions could be used, too.
I'm testing over and over again and if they don't exist in the form I'd know about it. As I'm sure that they exist is it neccessary to test for them using the PHP code?
Yes, because you cannot be sure that they exist. Never trust user input. Don't assume it will always be there, and never assume that it will be what it's supposed to be. Such assumptions lead to broken code and security issues.
I don't understand the advantages to doing this in my current situation. Are there any?
One should ensure that elements exist in arrays before attempting to access them. It's simply how things should be done. At the very least, it avoids the unnecessary generation of log entries.
And if the e-mail address isn't actually an e-mail address, or the name is just white space?
Ok, so what alternative should I know about?
As I wrote previously: regular expressions. You can find code for validating e-mail addresses on the Web. Google is your friend. Simply ensuring that input contains more than white space can be implemented with:
PHP Code:
if (!isset($_POST['name'])
|| !preg_match('/\\S+/', ($name = trim($_POST['name'])))) {
/* Name missing or only white space */
}
/* The name can be referred to by $name */
If the input comes from query string data, substitute $_POST for $_GET.
You suggested this script:
...(string literal split for readability only)
PHP Code:
"Password reminder:\r\n\r\n\r\n"
. "Username: {$username}\r\n"
. "Password: monkey\r\n\r\n"
. 'Please log-in at <http://torema.com.br/cases.php>'
I didn't really understand what you meant by
string literal split.
Not much: if the string was written as one long literal, it would have been (more) difficult to read and would have caused horizontal scrolling within the code box. Instead, I split it into several - four - smaller literals and concatenated them using the string concatenation operator (.).
For example, I'm trying to write a
string (terminology?)
To define a few terms, a literal is a representation of some sort of value in source code. Each basic data type - primitive - has a literal form.
Strings are sequences of characters: text. They are usually included in quotes (' or "), though another form does exist. Variable expansion - replacing a variable name with its value - doesn't occur in the single-quote form of a string literal. That is,
PHP Code:
$foo = 'bar';
echo 'Value: $foo';
would output "Value: $foo" not "Value: bar". The range of escape sequences is also reduced in the single-quote form: only \\ (single backslash) and \' (single quote) are processed.
I generally use the single-quote form whenever possible as the PHP parser will process them slightly quicker due to their simplicity, but it doesn't really matter that much.
Numbers and booleans (true/false) also have literals forms:
PHP Code:
$hundred = 100;
$third = 0.66666;
$c = 3e8; // 300000000
$isReady = true;
called
$message1 and I find it convenient to split it into several lines but in doing so I'm causing errors. Anyone who likes could tell me what's causing them in the following code. All of this code is meant to be a definition of
$message1.
PHP Code:
$message1 = 'E-mail do Site da Pagina: Fale Conosco\r\n\r\n'
'Nome: {$name}\r\n'
'Cargo: {$company}\r\n'
'Empresa: {$company}\r\n'
'Cidade: {$city} / {$state}'
//if the country is anything other than Brasil state it
if ($country=='Brasil')
{ echo '\r\n\r\n';}
//otherwise leave it out
else
{ echo '\r\n\r\n';}
'Intressa: {$interest}\r\n'
//if there is a message echo it
if ($message=='')
{ echo '\r\n';}
else
{ echo 'Mensagem: '.$message.' \r\n';}
'Deseja Contato Por: {$contactType}\r\n'
'Horario de Contato: {$timeTable}\r\n\r\n'
'Fone: {$tel}\r\n'
'E-mail: {$email}\r\n'
'Site: www.{$site}\r\n';
You aren't concatenating each literal with the next. A full stop (period) is the operator used here.
PHP Code:
/* Notice the change in quotation mark. This is necessary to replace escape
* sequences with the respective character, and to perform variable expansion.
*/
$message1 = "E-mail do Site da Pagina: Fale Conosco\r\n\r\n"
. "Nome: {$name}\r\n"
. "Cargo: {$company}\r\n"
. "Empresa: {$company}\r\n"
. "Cidade: {$city} / {$state}";
/* If country is not Brazil, include it in the message. */
if ($country != 'Brazil') {
$message1 .= "Country: {$country}";
}
$message1 .= "\r\n\r\n"
. "Intressa: {$interest}\r\n";
/* If there is a user-defined message, add it. */
if ($message != '') {
$message1 .= "Mensagem: {$message}";
}
$message1 .= "\r\n"
. "Deseja Contato Por: {$contactType}\r\n"
. "Horario de Contato: {$timeTable}\r\n\r\n"
. "Fone: {$tel}\r\n"
. "E-mail: {$email}\r\n"
. "Site: www.{$site}\r\n";
You might want to choose a better name than $message1. 
There's another way to write the code above without if statements. Instead, one could use the conditional operator, which is almost like an if statement for use in expressions, but we can leave that for another time.
You'll notice there is another issue involved in this script. I"ve got no idea if I can use if-else statements and echos like this.
The if statements are fine, though as you can see, it interrupts the process of building the string a little. As for the echo construct, this is meant for sending the value of string as output. For server-side PHP, this means transmitting it to the browser.
I hope they at least give the impression of what it is I'm trying to do.
I believe so. 
Mike
Bookmarks