Tuesday 10 April 2012

Wordpress Short codes forms processing


When searching for information, as can be incorporated into WordPress forms, the results repeatedly point to one theme: the integration of contact forms. That was not sought, because I wanted to incorporate a self-designed form in my WordPress blog and analyze the data entered in this form in a PHP script to display a graphic.

First of all, I got gnuplot to mind, as well gnuplot for generating a plurality of suitable diagrams. As far as I know, supported gnuplot but unfortunately not the output of the graphics in a stream, as it is desirable for the output as an image in a PHP script would be, but must always be realized through the detour of an output file, which in a multi-user environment, such as a website a few problems inevitable.

The second approach was to use the Google Chart API to a corresponding graphic display. Have a short flight over the document gave assurance that the venture with Google Chart API is possible; however, the parameters of the graphics as URL parameters are transmitted. That contradicted, however, my idea of how much you need to know about Google. Google had when the corresponding side slightly different cookies that have been placed on their side on your computer already, with your birth date entered here can match.

So there was only his own generation of graphics. Gnuplot output stream from a lack of difference to PHP. The PHP GD library module into the foreground. Although the creation of a chart with the help of the GD library is not just "dedicated optimized", but remains nevertheless limited in terms of cost. Simple lines and grids can be realized to a very reasonable cost. Especially since the GD library can be enabled as a module in PHP and supports the direct stream output - so graphics can be printed directly from a PHP script.

Realization

Shortcode
First of all, I've got a new short code defined that implements a form to enter your birth date. To change the formatting of the output I have not thought about, because the task is the installed WordPress themes and this is the task perfectly mastered.

The entered data is stored by the form (after verification) in the PHP global $ _SESSION array. A transfer to the graphic artist compelling script by URL was not an option, because that would have modified the parameters of a possible aggressor and to overload (by continuous multiple views) can be abused. Likewise, there are plugins for every major browser, which transmit the URL be surfed only once to a service (which may be well meant) to check the safety of the respective links. Such transmission of your date of birth I wanted to avoid at all costs. Therefore, only came to save the entered data in the session variable in question. And for your protection is to store the session data is limited to the duration of your browsing session. Once you close your browser, the site no longer knows what data you have entered into this form.



The issued by the Short Form Code has as its goal (when you click the submit button) itself The following should be recognized:

the form was filled out (values ​​submitted with the appropriate name)?
correspond to the values ​​transmitted to the default values ​​(they are valid)?
If so, then store the values ​​in the PHP variable $ _SESSION ['...']
If so, then embedding a URL that will contain an image as output



if (isset ($ _SESSION ['bio_day']) && isset ($ _SESSION ['bio_month']) && isset ($ _SESSION ['bio_year']))
{
        $ Bday = $ _SESSION ['bio_day'];
        Bmonth $ = $ _SESSION ['bio_month'];
        Byear $ = $ _SESSION ['bio_year'];
}

if (isset ($ _POST ['bio_day']) && isset ($ _POST ['bio_month']) && isset ($ _POST ['bio_year']))
{
        $ Tday = intval ($ _POST ['bio_day']);
        $ Tmonth = intval ($ _POST ['bio_month']);
        $ Tyear = intval ($ _POST ['bio_year']);
        if ($ tday> = 1 && $ tday <= 31 && $ tmonth> = 1 && $ tmonth <= 12 && $ tyear> = date ('Y') - 100 && $ tyear <= date ('Y') && checkdate ($ tmonth, $ tday, $ tyear))
        {
                $ = $ Bday tday;
                $ = $ Bmonth tmonth;
                $ = $ Byear tyear;
                $ _SESSION ['Bio_day'] = $ bday;
                $ _SESSION ['Bio_month'] = $ bmonth;
                $ _SESSION ['Bio_year'] = $ byear;
        }
}





PHP Script
The embedded PHP script to generate the graphics then verifies the existence of the first values. It must be given no parameters in the URL, as has been previously stored after successful validation all the required values ​​in session variables. The PHP script is the source of an image in the form <img source="myphpscript.php" /> indicated.

When calling the script myphpscript.php (from the same domain!) then all $ _SESSION variables to read again and can be further processed. With the help of the GD library can then generate corresponding graphics.

The result of this work is available on the site biorhythms are tested.

0 comments:

Post a Comment