PHP


PHP& Code Snippets09 Apr 2005 07:48 am

The following will allow POST through PHP as if it was submitted from a form in a browser.

<?php
$host = ‘www.sitetopostto.com’; // domain only, no path info
$path = ‘/path/to/program.cgi’; // path to cgi, asp, php program
$user_agent = ”; // identify as your own user agent (like ‘MSIE’) if you want

$fp = fsockopen($host, 80);
fputs($fp, “POST $path HTTP/1.1\r\n”);
fputs($fp, “Host: $host\r\n”);
fputs($fp, “Content-type: application/x-www-form-urlencoded\r\n”);
fputs($fp, “Content-length: ” . strlen($parameters) . “\r\n”);
if ($user_agent) {
fputs($fp, “User-Agent: $user_agent\r\n”);
}
fputs($fp, “Connection: close\r\n\r\n”);
fputs($fp, $parameters);

while (!feof($fp)) {
$data .= fgets($fp,128);
}
fclose($fp);

// (OPTIONALLY strip header returned (for XML response only), use $data otherwise
ereg(”<(.+)”,$data,$matched);
$without_header_data = ‘<’ . $matched[1];
?>

PHP& Code Snippets07 Apr 2005 03:33 pm

To get the output of a parsed file (without printing to the screen):

<?
// Grab output of file
ob_start();
include(”parsed_file.php”);
$test_data = ob_get_contents();
ob_end_clean();

// Write to a new file if you like
if($fh = fopen(”new_file.html”, “w”)){
  fputs($fh, $test_data);
  fclose($fh);
}
?>

PHP& Code Snippets06 Apr 2005 12:05 pm

Very basic XML parser example using PHP and the ‘–with-xml’ option.

<?php
$parser = xml_parser_create();
xml_parse_into_struct($parser,$xml,&$structure,&$index);
xml_parser_free($parser);
$i = 0;
foreach($structure as $s) {
if($s[”level”] == 2) {
// load results into $res variable
$res[$i] = $s[”value”];
$i++;
}
}
?>

##
The example above gets the level 2 value. You can do a var_dump($structure) to get the entire document/node structure.

PHP03 Apr 2005 10:18 am

PHP: Downloads:

The PHP 5.0.4 source packages were re-released due to a missing file in the embedded PEAR distribution. There are no changes in this re-release other than the addition of the missing file.

PHP& Code Snippets31 Mar 2005 11:31 am

I came across this great REGEX calculator

PHP& Code Snippets27 Mar 2005 09:11 am

/* web convert date to unix_timestamp: www.4webhelp.net/us/timestamp.php */

One can create their own unix_timestamp converter using mktime() function.

// subtract old timestamp
$t_sec = time() - 1102971600;

/*
divide #seconds by 60 for minutes
divide #of minutes by 60 for hours
divide #of hours by 24 for days
use floor to get integer for day
*/
$t_day = floor(($t_sec / 60) / 60 / 24);

PHP& Code Snippets22 Mar 2005 06:50 pm

Example.

// redirects a user to scriptschool.com
header(”Location: http://www.scriptschool.com/”);

Also see PHP course I, week #8.

PHP& JavaScript& HTML CSS& MySQL21 Mar 2005 08:19 pm

Just submitted an article with 10 tips for speeding up webpages to Cyberwurx:

This is an impatient net society as netizens crave more bandwidth. It seems logical to think that the hosting company are the primary people to contact when pages load slowly, but there are actually a lot of things that we, as webmasters, can do first and foremost to make sure that our web pages load faster before contacting the hosting company. Sometimes very small changes we make with our habits as webmasters can make huge impacts in the speed of which our web pages load particularly in the content of the pages, the compression and third party programs being accessed/used. Today I’m going to detail some specific items below that will help speed up web pages…

Everything from writing your own HTML, optimizing images all the way to the more advanced concepts of employing caching is discussed.

Happy coding to you!

PHP& Code Snippets18 Mar 2005 02:40 pm

How to pad numbers with leading zeros using PHP:

Example #1:

$num = 5;
$count = sprintf(”%05s”,$num); // produces ‘00005′

Example #2:
$num = 5310;
$count = sprintf(”%05s”,$num); // produces ‘05310′

« Previous Page