php-general Digest 20 Jun 2012 13:55:58 -0000 Issue 7861
Topics (messages 318276 through 318279):
Re: else if vs switch
318276 by: James
318277 by: April Mains
318278 by: Ashley Sheridan
php script can't self reference
318279 by: Tim Dunphy
Administrivia:
To subscribe to the digest, e-mail:
php-general-digest-subscr...@lists.php.net
To unsubscribe from the digest, e-mail:
php-general-digest-unsubscr...@lists.php.net
To post to the list, e-mail:
php-gene...@lists.php.net
----------------------------------------------------------------------
--- Begin Message ---
>---- Original Message ----
>From: April Mains <aprilma...@gmail.com>
>To:
>Cc: "PHP-General list" <php-gene...@lists.php.net>
>Sent: Mon, Jun 18, 2012, 9:41 AM
>Subject: Re: [PHP] else if vs switch
>
>This is what I had been using as the check based on the code that had been
>there previously and along with an email validator that sets $email to ""
>if the address isn't valid. The purpose of the form is lead generation. The
>last bit is to prevent spammers from entering urls in the class text box.
>
>iif (($name == "") || ($email == "") || ($phone =="") || ($city=="Select
>your city") || ($class=="") ||
>preg_match("/[^A-Za-z0-9-\\s\\(\\)\\?\\:\\;@\\.™\\,\\–\\&'\\t]/uis",
>$class))
>{...}
>
>Does this do the same thing as isset? Would isset be better?
>
>April
>
>On Sun, Jun 17, 2012 at 7:41 PM, James <ja...@nixsecurity.org> wrote:
>
>> Same logical check with my personal preference ;)
>>
>> $toaddress = $mapping['default'];
>>
>> if ( isset($city) && isset($mapping[$city]) ) { ... }
>>
>> --
>> Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>>
>> Jim Lucas <li...@cmsws.com> wrote:
>>>
>>> On 6/15/2012 3:29 PM, Joshua Kehn wrote:
>>> > Way easier to just use a map.
>>> >
>>> > $mapping = array(
>>>
>>> > 'Calgary' => "abc@emailaddress",
>>> > 'Brooks' => "def@emailaddress",
>>> > // etc
>>> > );
>>> > $toaddress = $mapping[$city];
>>>
>>> I would use this, but add a check to it.
>>>
>>> $mapping = array(
>>> 'default' => 'defa...@domain.tld',
>>> ...
>>> );
>>>
>>> ...
>>>
>>> if ( isset($mapping[$city]) ) {
>>> $toaddress = $mapping[$city];
>>>
>>> } else {
>>> $toaddress = $mapping['default'];
>>>
>>> }
>>>
>>> Jim
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>>>
Technically, no, that's not the same as using isset(). The isset() function
determines if the variable is set and is not NULL. Your checks, for example,
will throw a PHP Notice if any one of those variables were never initialized in
the scope of your script. If you're not going to initialize your variables
before performing the check, then using isset() would be ideal. However, for
the sake of correctness, I'd recommend you either initialize your variables or
use isset(). Take a look at the empty() function in PHP, it may suit your
needs. http://us3.php.net/manual/en/function.empty.php.
PHP Notice message example:
[18-Jun-2012 13:43:20 UTC] PHP Notice: Undefined variable: name in
/root/test.php on line 2
Example of variable initialization
$name = "";
$email = "";
$phone = "";
$city = "Select your city";
$class = "";
James
--- End Message ---
--- Begin Message ---
On 2012-06-18, at 7:59 AM, James wrote:
>> ---- Original Message ----
>> From: April Mains <aprilma...@gmail.com>
>> To:
>> Cc: "PHP-General list" <php-gene...@lists.php.net>
>> Sent: Mon, Jun 18, 2012, 9:41 AM
>> Subject: Re: [PHP] else if vs switch
>>
>> This is what I had been using as the check based on the code that had been
>> there previously and along with an email validator that sets $email to ""
>> if the address isn't valid. The purpose of the form is lead generation. The
>> last bit is to prevent spammers from entering urls in the class text box.
>>
>> iif (($name == "") || ($email == "") || ($phone =="") || ($city=="Select
>> your city") || ($class=="") ||
>> preg_match("/[^A-Za-z0-9-\\s\\(\\)\\?\\:\\;@\\.™\\,\\–\\&'\\t]/uis",
>> $class))
>> {...}
>>
>> Does this do the same thing as isset? Would isset be better?
>>
>> April
>>
>> On Sun, Jun 17, 2012 at 7:41 PM, James <ja...@nixsecurity.org> wrote:
>>
>>> Same logical check with my personal preference ;)
>>>
>>> $toaddress = $mapping['default'];
>>>
>>> if ( isset($city) && isset($mapping[$city]) ) { ... }
>>>
>>> --
>>> Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>>>
>>> Jim Lucas <li...@cmsws.com> wrote:
>>>>
>>>> On 6/15/2012 3:29 PM, Joshua Kehn wrote:
>>>>> Way easier to just use a map.
>>>>>
>>>>> $mapping = array(
>>>>
>>>>> 'Calgary' => "abc@emailaddress",
>>>>> 'Brooks' => "def@emailaddress",
>>>>> // etc
>>>>> );
>>>>> $toaddress = $mapping[$city];
>>>>
>>>> I would use this, but add a check to it.
>>>>
>>>> $mapping = array(
>>>> 'default' => 'defa...@domain.tld',
>>>> ...
>>>> );
>>>>
>>>> ...
>>>>
>>>> if ( isset($mapping[$city]) ) {
>>>> $toaddress = $mapping[$city];
>>>>
>>>> } else {
>>>> $toaddress = $mapping['default'];
>>>>
>>>> }
>>>>
>>>> Jim
>>>>
>>>> --
>>>> PHP General Mailing List (http://www.php.net/)
>>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>>
>>>>
>>>>
>
>
> Technically, no, that's not the same as using isset(). The isset() function
> determines if the variable is set and is not NULL. Your checks, for example,
> will throw a PHP Notice if any one of those variables were never initialized
> in the scope of your script. If you're not going to initialize your variables
> before performing the check, then using isset() would be ideal. However, for
> the sake of correctness, I'd recommend you either initialize your variables
> or use isset(). Take a look at the empty() function in PHP, it may suit your
> needs. http://us3.php.net/manual/en/function.empty.php.
>
> PHP Notice message example:
> [18-Jun-2012 13:43:20 UTC] PHP Notice: Undefined variable: name in
> /root/test.php on line 2
>
> Example of variable initialization
>
> $name = "";
> $email = "";
> $phone = "";
> $city = "Select your city";
> $class = "";
>
> James
>
Sorry I didn't include the whole script. The variables as they come in from a
form as follows:
//initialize variables
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$city=$_POST['city'];
$class=$_POST['class'];
Thus its not throwing an errors.
Thank you for all your help with this. Its given me lots to think about.
April
--- End Message ---
--- Begin Message ---
April Mains <aprilma...@gmail.com> wrote:
>
>On 2012-06-18, at 7:59 AM, James wrote:
>
>>> ---- Original Message ----
>>> From: April Mains <aprilma...@gmail.com>
>>> To:
>>> Cc: "PHP-General list" <php-gene...@lists.php.net>
>>> Sent: Mon, Jun 18, 2012, 9:41 AM
>>> Subject: Re: [PHP] else if vs switch
>>>
>>> This is what I had been using as the check based on the code that
>had been
>>> there previously and along with an email validator that sets $email
>to ""
>>> if the address isn't valid. The purpose of the form is lead
>generation. The
>>> last bit is to prevent spammers from entering urls in the class text
>box.
>>>
>>> iif (($name == "") || ($email == "") || ($phone =="") ||
>($city=="Select
>>> your city") || ($class=="") ||
>>>
>preg_match("/[^A-Za-z0-9-\\s\\(\\)\\?\\:\\;@\\.™\\,\\–\\&'\\t]/uis",
>>> $class))
>>> {...}
>>>
>>> Does this do the same thing as isset? Would isset be better?
>>>
>>> April
>>>
>>> On Sun, Jun 17, 2012 at 7:41 PM, James <ja...@nixsecurity.org>
>wrote:
>>>
>>>> Same logical check with my personal preference ;)
>>>>
>>>> $toaddress = $mapping['default'];
>>>>
>>>> if ( isset($city) && isset($mapping[$city]) ) { ... }
>>>>
>>>> --
>>>> Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>>>>
>>>> Jim Lucas <li...@cmsws.com> wrote:
>>>>>
>>>>> On 6/15/2012 3:29 PM, Joshua Kehn wrote:
>>>>>> Way easier to just use a map.
>>>>>>
>>>>>> $mapping = array(
>>>>>
>>>>>> 'Calgary' => "abc@emailaddress",
>>>>>> 'Brooks' => "def@emailaddress",
>>>>>> // etc
>>>>>> );
>>>>>> $toaddress = $mapping[$city];
>>>>>
>>>>> I would use this, but add a check to it.
>>>>>
>>>>> $mapping = array(
>>>>> 'default' => 'defa...@domain.tld',
>>>>> ...
>>>>> );
>>>>>
>>>>> ...
>>>>>
>>>>> if ( isset($mapping[$city]) ) {
>>>>> $toaddress = $mapping[$city];
>>>>>
>>>>> } else {
>>>>> $toaddress = $mapping['default'];
>>>>>
>>>>> }
>>>>>
>>>>> Jim
>>>>>
>>>>> --
>>>>> PHP General Mailing List (http://www.php.net/)
>>>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>>>
>>>>>
>>>>>
>>
>>
>> Technically, no, that's not the same as using isset(). The isset()
>function determines if the variable is set and is not NULL. Your
>checks, for example, will throw a PHP Notice if any one of those
>variables were never initialized in the scope of your script. If you're
>not going to initialize your variables before performing the check,
>then using isset() would be ideal. However, for the sake of
>correctness, I'd recommend you either initialize your variables or use
>isset(). Take a look at the empty() function in PHP, it may suit your
>needs. http://us3.php.net/manual/en/function.empty.php.
>>
>> PHP Notice message example:
>> [18-Jun-2012 13:43:20 UTC] PHP Notice: Undefined variable: name in
>/root/test.php on line 2
>>
>> Example of variable initialization
>>
>> $name = "";
>> $email = "";
>> $phone = "";
>> $city = "Select your city";
>> $class = "";
>>
>> James
>>
>
>
>Sorry I didn't include the whole script. The variables as they come in
>from a form as follows:
>
>//initialize variables
>$name=$_POST['name'];
>$email=$_POST['email'];
>$phone=$_POST['phone'];
>$city=$_POST['city'];
>$class=$_POST['class'];
>
>Thus its not throwing an errors.
>
>Thank you for all your help with this. Its given me lots to think
>about.
>
>April
>
>
>
>
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php
If they're coming from the $_POST array you should be doing the asset() there:
$name = (isset($_POST['name']))?$_POST['name']:'';
This is because the you can never rely on user data, even when you think its
coming from a form you built. It may cone from another source or something may
get changed which breaks the receiving code.
Thanks,
Ash
http://ashleysheridan.co.uk
--- End Message ---
--- Begin Message ---
Hello list,
I just wanted to bounce a slight issue that I'm having off you
regarding self referencing a php script. Moving from the
'sendemail.htm' page where a form is used to the 'sendemail.php' page
that is in the form action works fine! But if you reload the page, the
php part of the equation loses track of it's $_POST[] variables, and
you see the following errors in the output of the php page:
Notice: Undefined index: subject in
/Library/WebServer/Documents/examples/ch03/final/makemeelvis/sendemail.php
on line 19
Notice: Undefined index: elvismail in
/Library/WebServer/Documents/examples/ch03/final/makemeelvis/sendemail.php
This is the original form from the sendemail.html page:
<form method="post" action="sendemail.php">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text" size="30" /><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8"
cols="40"></textarea><br />
<input type="submit" name="Submit" value="Submit" />
</form>
On the php page I'm using a true / false 'flag' to indicate whether or
not a form is displayed.
$output_form = "false";
If the user fails to submit information to either field the script
re-displays the form via the $output_form variable:
if (empty($subject) && empty($text)) {
//* We know both $subject AND $text are blank
echo 'You forgot the email subject and body.<br />';
$output_form="true";
}
if ($output_form == 'false') {
$dbc = mysqli_connect('127.0.0.1', 'admin', 'secret', 'elvis_store')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
while ($row = mysqli_fetch_array($result)){
$to = $row['email'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$msg = "Dear $first_name $last_name,\n$text";
mail($to, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $to . '<br />';
}
mysqli_close($dbc);
}
The html form is really the only important part of the .html page, but
here is the full php script from 'sendemail.php' to provide a bit of
context. Passwords have been changed to protect the innocent!
<?php
$from = 'm...@mydomain.com';
$subject = $_POST['subject'];
$text = $_POST['elvismail'];
$output_form = "false";
if (empty($subject) && empty($text)) {
//* We know both $subject AND $text are blank
echo 'You forgot the email subject and body.<br />';
$output_form="true";
}
if (empty($subject) && !empty($text)) {
echo 'You forgot the email subject.<br />';
$output_form="true";
}
if ((!empty($subject)) && empty($text)) {
echo 'You forgot the email body text.<br />';
$output_form="true";
}
if ($output_form == 'true') {
echo '<br /><br /><form action="sendemail.php" "method="post">
<label for="subject">Subject of email:</label><br />
<input id="subject" name="subject" type="text" size="30" /><br />
<label for="elvismail">Body of email:</label><br />
<textarea id="elvismail" name="elvismail" rows="8"
cols="40"></textarea><br />
<input type="submit" name="Submit" value="Submit" />
</form>';
}
if ($output_form == 'false') {
$dbc = mysqli_connect('127.0.0.1', 'admin', 'secret', 'elvis_store')
or die('Error connecting to MySQL server.');
$query = "SELECT * FROM email_list";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
while ($row = mysqli_fetch_array($result)){
$to = $row['email'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];å
$msg = "Dear $first_name $last_name,\n$text";
mail($to, $subject, $msg, 'From:' . $from);
echo 'Email sent to: ' . $to . '<br />';
}
mysqli_close($dbc);
}
?>
Thanks in advance!
tim
--
GPG me!!
gpg --keyserver pool.sks-keyservers.net --recv-keys F186197B
--- End Message ---