TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 11-11-2008, 03:52 PM   #1 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default else problem

Anyone detect the problem here?

PHP Code:
if ($name '' or $phone ='' or $email '' or $message ''){
    
header("Location: http://www.url.com/error.php");
} else { (
mail($recipient$subject$body$header))
      
header("Location: http://www.url.com/thanks.php");

I'm getting an error in the 2nd header...
9three is offline  
Reply With Quote
Old 11-11-2008, 04:24 PM   #2 (permalink)
The Addict
 
tony's Avatar
 
Join Date: Aug 2008
Posts: 312
Thanks: 8
tony is on a distinguished road
Default

I am not sure now, what could be. can you tell us what type of error do you get?

also restructuring a little the code:

PHP Code:
if (is_null($name) || is_null($phone) || is_null($email) || is_null($message)){
    
header("Location: http://www.url.com/error.php");
} else { 
    if(
mail($recipient$subject$body$header))
        
header("Location: http://www.url.com/thanks.php");

that might help, maybe the error carry over from the mail function being inside parenthesis
tony is offline  
Reply With Quote
Old 11-11-2008, 04:25 PM   #3 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

yeah, you should TEST the conditions, and not make assignments. therefore...

PHP Code:
if($a 'b'
will become:

PHP Code:
if($a == 'b'
Also, you should use the OR operand ( || ) instead of the keyword or, because it has greater precedence.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 11-11-2008, 04:51 PM   #4 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

I changed to || instead of OR. I also added the IF inside the {} and it fixed it.

I changed the code to

PHP Code:
if (is_null($_POST['name']) || is_null($_POST['phone']) || is_null($_POST['email']) || is_null($_POST['message'])){
    
header("Location: http://www.url.com/error.php");
} else { if (
mail($recipient$subject$body$header))
      
header("Location: http://www.url.com/thanks.php");

The email is sent out whether they fill it out or not. And it always sends the sender to thanks.php instead of error.php when its null
9three is offline  
Reply With Quote
Old 11-11-2008, 05:02 PM   #5 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

It would be better to do the last part this way:

PHP Code:
elseif( mail$recipient$subject$body$header ) )
{
   
header'Location: http://www.url.com/thanks.php' );

Unless you are going to do something like the following:

PHP Code:
if( your first if )
{
   
// some stuff
}
else
{
   if( 
mail$recipient$subject$body$header ) )
   {
      
header'Location: http://www.url.com/thanks.php' );
   }

   else
   {
      
// failed to send mail
   
}

Send a message via MSN to Runar
Runar is offline  
Reply With Quote
Old 11-11-2008, 05:36 PM   #6 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

the top part of my code is just assigning variables to my form.

I tried out the first code you gave me

PHP Code:
elseif( mail$recipient$subject$body$header ) )
{
   
header'Location: http://www.url.com/thanks.php' );

it sends me straight to my thanks.php whether its null or not.

The 2nd code you put is what tony suggested. which i tried and it sent me to thanks.php whether null or not.
9three is offline  
Reply With Quote
Old 11-11-2008, 05:47 PM   #7 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

I know. The mail() function will actually return true (or attempt to send the mail, if I understood it correctly) even if the parameters submitted are 0 or not valid at all.

I would personally use empty() instead of is_null(), since is_null() will return false even if the variable is '' or 0.

PHP Code:
if( !empty( $_POST['subject'] ) && !empty( $_POST['message'] ) )
{
   if( 
mail'my@mail.com'$_POST['subject'], $_POST['message'] ) )
   {
      
// Success!
   
}

   else
   {
      
// Failure!
   
}
}

else
{
   
// One or more of the required fields are not set

Edit: Try to run the following code, then submit the form without filling any fields, and you will see why using is_null() is a bad idea in such cases:

PHP Code:
<?php
if( isset( $_POST['submit'] ) )
{
    
$r print_r$_POSTTRUE );
    echo 
'<pre>' $r '</pre>';
    
    if( 
is_null$_POST['number_1'] ) )
    {
        echo 
'is_null';
    }
}
?>
<form action="/test.php" method="post">
    <input type="text" name="number_1" />
    <input type="text" name="number_2" />
    
    <button type="submit" name="submit">Submit</button>
</form>
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
Old 11-11-2008, 05:57 PM   #8 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

nope its not working !empty will still send it out :(
9three is offline  
Reply With Quote
Old 11-11-2008, 06:00 PM   #9 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

Quote:
Originally Posted by 9three View Post
nope its not working !empty will still send it out :(
Then you are doing something wrong when setting the variables. Could you post your entire script, at least the parts where you are setting the variables?
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
The Following User Says Thank You to Runar For This Useful Post:
9three (11-11-2008)
Old 11-11-2008, 06:20 PM   #10 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

PHP Code:
<?php

$name 
$_POST['name']; //senders name
$phone $_POST['phone'];
$website $_POST['website'];
$email $_POST['email']; //senders e-mail adress
$recipient "info@url.com"//recipient
$message $_POST['message']; //mail body
$subject "Client Request"//subject
$header "From: "$name " <" $email ">\r\n"//optional headerfields
$body "Name: "$name ."

         Phone: "
.$phone.
         
         Website: "
.$website.
         
         Email:"
.$email.
         
         Message: "
.$message."\r\n";
         
if ( !empty(
$_POST['name']) || !empty($_POST['phone']) || !empty($_POST['email']) || !empty($_POST['message'])){
    
header("Location: http://www.url.com/error.php");
} else { if (
mail($recipient$subject$body$header))
      
header("Location: http://www.url.com/thanks.php");
}

?>
9three is offline  
Reply With Quote
Old 11-11-2008, 06:58 PM   #11 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

Try this code. Either was my explanation poor, or you misunderstood what I meant about using !empty(). Anyway, try this code and let me know what you get:

PHP Code:
<?php

// Display all errors
ini_set'display_errors''yes' );

// Post data
$name      $_POST['name'];
$phone     $_POST['phone'];
$website   $_POST['website'];
$email     $_POST['email'];
$message   $_POST['message'];

// Define some mail variables
$recipient "info@url.com";
$subject   'Client Request';
$header    'From: ' .  $name ' <' $email '>' "\r\n";
$body      'Name: ' $name "\n\nPhone: " $phone "\n\nWebsite: " $website "\n\nEmail: " $email "\n\nMessage: " $message;

// Check that all required fields (name, phone, email, message) are set
if( !empty( $name ) && !empty( $phone ) && !empty( $email ) && !empty( $message ) )
{
    
// Attempt to send mail
    
if( mail$recipient$subject$body$header ) )
    {
        
header'Location: /thanks.php' );
    }
    
    
// Failed
    
else
    {
        
header'Location: /mailfail.php' );
    }
}

// One or more of the required fields were missing, redirect to error page
else
{
    
header'Location: /error.php' );
}

?>
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
Old 11-11-2008, 07:40 PM   #12 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

I guess I was confused with the whole if {} thing. Yes it works now :) thanks
9three is offline  
Reply With Quote
Old 11-11-2008, 08:12 PM   #13 (permalink)
The Contributor
 
Runar's Avatar
 
Join Date: Nov 2008
Location: Norway
Posts: 58
Thanks: 20
Runar is on a distinguished road
Default

I am glad I could help! :)
Send a message via MSN to Runar
Runar is offline  
Reply With Quote
Old 11-12-2008, 10:04 AM   #14 (permalink)
The Acquainted
 
EyeDentify's Avatar
 
Join Date: Nov 2007
Location: Sweden
Posts: 106
Thanks: 13
EyeDentify is on a distinguished road
Default

@9three

From this thread i never grasped if you understood the difference between:

Assigning:
PHP Code:
<?PHP $myVar 'myString'?>
And comparison:
PHP Code:
<?PHP $myVar == 'myString'?>
In the first you assign the string to $myVar
In the last you check to see if the $myVar has the same value as the string.

Itīs an easy misstake, i do it often myself.
Anyways :) good luck.
__________________
Of course the whole point of a doomsday machine, would have been lost if you keep it a secret.
EyeDentify is offline  
Reply With Quote
Old 11-12-2008, 09:32 PM   #15 (permalink)
The Contributor
 
Join Date: Oct 2008
Posts: 75
Thanks: 4
9three is on a distinguished road
Default

yea i have = and == covered :D
9three is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 05:09 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design