Software Code

Cookies in Javascript: Handling Session Information

Cookies written using Javascript is a technique that enables a wensite owner to store information about a given user for submission in a form, or so manipulation at a later browsing session by the same use. Being that cookies are stored on the user's machine (client-side), they do not present a fail-safe way to handle information, but they are essential in accomplishing this task in situations where the web programmer does not have access so server-side programming and scripting plateforms such as ASP, JSP and PHP (just to mention a few).

Patching files using DIFF/PATCH in windows

If you are maintaining any application, especially the pervasive opensource applications that are constantly changing, you need to periodically patch your application. Int he Linux/Unix world, it is second nature to make changes generate a DIFF file, and apply a patch. But when you are using Windows,and are chronically used to a GUI for everything, this can be a stumbling block. I will admit that for a long time, I either ignored patches and just turned off unnecessary features to avoid security compromises, or manually sifted through the patch.file and applied the patch by hand.

There are some applications that make it as easy to patch applications in Windows. GNUWin32 is a freely available application for windows that you can use to apply patches from the command prompt. This is downloadable from http://gnuwin32.sourceforge.net/packages/patch.htm
Once downloaded and installed, you will need to add the folder structure for this application to your Windows %PATH% settings from in the Windows configuration (You should know this as a good Windows use, or access it from the control Panel). This will allow you to call the patch application from any folder (you will need to reboot Windows once you modify the %PATH% settings).

Patching files

Once you have your patch application installed and working, you can use the following command sequence to patch your files

patch -b original.file patch.file

This simple command will create a backup of the original.file, apply the patch using the specified patch.file

demo of a simple patch operation from the Windows Command-prompt

NB: The patch application comes with a useful and complete listing of all the switches that you can use (like -b) to make the application do certain things as it patches one or more files

Sending a PHP rediect header (301,302,303 etc) with an embedded variable

As explained in a related document and in many other reviews of processes and code on he website, you can create a redirect or send a Header from PHP with any-one of the 301, 302, 303, etc redirect headers. I will not go into the uses of these kinds of redirects (handled in a different document). The purpose of this document is to add to the already described code for generating these headers.

It is not always that you will know where you want to point the user to. Sometimes you may want to use a variable to determine where the user should be pointed to when they visit a page. For example, within the Drupal CMS, and any other automated system, I needed to create a menu link to a value declared in the profile of a given user. The problem was that you can only call that variable easily from the profile page of a user, and not from auxiliary pages. I could however call the profile page from the auxiliary pages. My strategy was therefore to link back to the profile page with a querystring value telling my code there that I want to pick a value from the profile array (available on the profile page), and use that value in the redirect.

Steps to the solution
My approach was to call the profile page (/user/UID?target=my_site).
On the profile page, I put some PHP code to detect this querystring and build a 301 or 302 redirectheader based on the value pulled from the profile page.

<?php
$my_site = $user->profile_mysite;
if (($_GET["target"])==("my_website")) {
header("HTTP/1.1 301 Moved Permanently");
header("Location: ".$my_site);
exit();
}
?>

Ths approach can e used in many other instances (for you to think them up). I ran into this problem long time ago with classic ASP and it was nto able to hanle this without some tedius coding... unless my knowledge or ASP (or lack thereof) was the stumbling block

PHP/Drupal code to generate CMS content as XML

There are two steps to the process:
- Trigger the alternative theme that writes XML
- Write the theme that renders XML

1)
In your page.tpl.php, add this code at the top

<?php
if (($_GET["format"])==("xml")) {
include 'xml.page.tpl.php'; /*call the alternative template file */
return; }
?>

In my case, I am looking to see if there is a querystring format=xml as a condition

Do the same for node.tpl.php and this time point to an alternative node.tpl.php

<?php
if (($_GET["format"])==("xml")) {
include 'xml.node.tpl.php'; /*call the alternative template file */
return; }
?>

2) The template files:
Example xml.page.tpl.php and xml.node.tpl.php (the naming format is arbitrary and can be anything)

<?php
$xml_header='<?xml version="1.0" encoding="ISO-8859-1"?>';
header('Content-type: text/xml');
print $xml_header;
?>
<drupal_page>
<?php print $content; ?>
</drupal_page>

and

<node>
<link><?php print $node_url ?></link>
<title><?php print $title ?></title>
<content><?php print $content ?></content>
</node>

There you go, test the code and correct any coding errors and it should work (I assume that you have already tested the implementation on cmsproducer.com). If you need help with this, please ask.

Please note that this code is just an example of greater things that you can accomplish by creatively theming CMS data. You call pull-in more fields, and even take the resultant XML and manipulate it using XSLT, or consume it into Adobe flash for presentation... If you come up with some creative application, please share them with th rest of us by commenting on this website, or on Drupal.org: Drupal to XML: By theme, Server-side manipulation

Valid XHTML 1.0 Strict
This site is accepts Oped ID authentication for login
This Website is Built Using Semantic Markup and Cascading Style Sheets (CSS)
Some usage rights are reserved, please contact us for approval before using it