PHP - Bits & Pieces

PHP programming codes and scripting are discussed in the manner of use and relevance. Examples of PHP syntax and scripting are presented in concise writups for better understanding.

Wednesday, June 4, 2008

Using PHP to submit a form to itself

In an earlier post, we see how php can be used to check if the form has been submitted:


http://bitspiecesphp.blogspot.com/2008/05/using-php-to-check-is-form-has-been.html


The following code indicates how to sumbit the page to itself. Just include the php code in the action attribute of the form tag of your html. The server will resubmit the page to itself when activated.


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

Tuesday, June 3, 2008

Use PHP to track email sender from your web site

How do you know the emails that you received through your web site html form came from a trust worthy source? Often we received machines sent messages and falls victim to spamming on our email account. One way to prevent this and track spamming is to get the sender's IP, page of reference (the url link that refers the sender to to your email page) and user agent (browser).

Example of Use:


On your email form page include the following codes:


$ipi = getenv("REMOTE_ADDR"); // IP address.
$httprefi = getenv ("HTTP_REFERER"); // Url link of Reference Page.
$httpagenti = getenv ("HTTP_USER_AGENT");// Browser info.

//send for processing through your html form. Hide them in hidden fields so that only you get the information on the sender.
<input type="hidden" name="ip">
<input type="hidden" name="httpref">
<input type="hidden" name="httpagent">


When processing the form, you may want to check that all the 3 inputs are not empty before you continue. If they are, it most likely that they are send by a machine.

Wednesday, May 28, 2008

Using PHP to solve the session_start() Error

This is a commonly occurred error in PHP programming. It is usually caused by the session start script. When a separate header file is included at the top of the page, the error is caused by duplicate HTML tag or white space before the session start script. This happen when the web page try to resend headers information

Error:

Warning: session_start() [function.session-start]: Cannot send session cache limiter - headers already sent


Solution:

Place the following script at the top of the page:
<?php
ob_start();
session_start();
?>


and this at the end of the page:
<?php // Flush the buffered output to the Web browser.
ob_flush();
?>

Tuesday, May 27, 2008

Using PHP to read posted form data

The first line below shows how form data is read by PHP. 'StsToAnInch' is the the input type name. In this case the input type is a text box. The second line is how the text box is written in html

$_POST['StsToAnInch']

Note: The code works with an input text field named "StsToAnInch".

Monday, May 26, 2008

Using PHP to add to strings or join string with string variables

In PHP programming, one constantly has to be reminded of how to add a string of text to an existing one dynamically. We first create a line of string text and assign it to a string variable. We add onto the string in the following manner:

$instn = $instn . "No shaping is needed from waist to underarm as their widths are the same.\n\n";

Remember to add the new string to the variable with '.' or else the new text will replace all previous text in the variable.

Friday, May 23, 2008

Using PHP to check if a form has been submitted

The 'if' condition in this PHP script is place in the same page as the html form and is located at the top of the page, above the form. The purpose is to verify the inputs of the form. Example of uses include the Login Page.

if (isset($_POST['submit'])) {

//run this code when the submit button is activated, name the button submit

}

Thursday, May 22, 2008

Using PHP to find Out if a Nummber is Odd or Even

This little bit of scripts was most useful in the coding of Waist Shaping Knitting Calculator . It evens out the number of knitting stitches so that both sides of the waist line get to be adjusted evenly.

The 'if' condition tests if variable '$Sts' is an odd number:

if( $Sts & 1 ){
$Sts = $Sts + 1;} //true when $Sts is odd, so add 1 to $Sts to make it even.


This bit of php will be useful for further development of the Knitting Calculator series.