Know whether the URL was rewritern or not

A simple function to detect if the current page address was rewritten by mod_rewrite:

<?php
function urlWasRewritten() {
$realScriptName=$_SERVER['SCRIPT_NAME'];
$virtualScriptName=reset(explode("?", $_SERVER['REQUEST_URI']));
return !($realScriptName==$virtualScriptName);
}
?>

Now, How to use this function? Continue reading

curl timeout problem and solution

If you want cURL to timeout in less than one second, you can use CURLOPT_TIMEOUT_MS, although there is a bug/”feature”  on “Unix-like systems” that causes libcurl to timeout immediately if the value is < 1000 ms with the error “cURL Error (28): Timeout was reached”.  The explanation for this behavior is:

“If libcurl is built to use the standard system name resolver, that portion of the transfer will still use full-second resolution for timeouts with a minimum timeout allowed of one second.”

What this means to PHP developers is “You can use this function without testing it first, because you can’t tell if libcurl is using the standard system name resolver (but you can be pretty sure it is)”

The problem is that on (Li|U)nix, when libcurl uses the standard name resolver Continue reading

Lets begin curl scripting

cURL is a library which allows you to connect and communicate to many different types of servers with many different types of protocols. Using cURL you can:

  • Implement payment gateways’ payment notification scripts.
  • Download and upload files from remote servers.
  • Login to other websites and access members only sections.

PHP cURL library is definitely the odd man out. Unlike other PHP libraries where a whole plethora of functions is made available, PHP cURL wraps up major parts of its functionality in just four functions.

A typical PHP cURL usage follows the following sequence of steps.

curl_init – Initializes the session and returns a cURL handle which can be passed to other cURL functions.

curl_opt – This is the main work horse of cURL library. This function is called multiple times and specifies what we want the cURL library to do.

curl_exec – Executes a cURL session.

curl_close – Closes the current cURL session.

Continue reading

White Screen of Death

Right now some situations (parse errors, undefined function call, no more memory) in PHP result in fatal error – which means the engine can not continue with the request beyond this point. From the user point of view, this often results in a blank page. I wonder if it would be possible to have standard recovery mechanism that would allow the PHP engine on fatal error to enter some kind of “recovery mode” when it would output some very basic page saying “ok, I have some problems here, but don’t panic and just tell my programmer to fix the code”. It won’t give much info probably but it would allow production sites display nice message to the users instead of the boring snowfield panorama it displays now (that is if the administrator was smart enough to set display_errors to off). Continue reading

My first cron

Few days ago when I was working on unseenapps, I came across a functionality which must be executed repeatedly after a certain interval of time. I thought of it and later find out the simplest way.
It was the cpanels in-built functionality called cron jobs.

So what a cron job is?
Well, a cron job is a cpanel module that allows you to run a certain command at times set by the jobs.
For example, you could set a cron job to delete temporary files every week so that your disk space is not being used by those files.

How to setup a cron jobs?
Basically you will want to run a PHP script file in specific intervals. Suppose you want to execute a php file called cronjob.php every one hour. This is what you do:

The CRON Command is in the Following Format

[ Minute – Hour – Day – Month – Weekday ] – Command Continue reading

Send files via FTP using php

Sending files to a server via FTP is an essential ability of any web developer or designer. Of course, we all use glossy FTP clients like WS_FTP and FireFTP, but what about FTP automation? You can use PHP to FTP files from one server to another. Let me show you how

<?php
$server = “server name”;
$ftp_user_name = “username”;
$ftp_user_pass = “password”;
$connection = ftp_connect($server);
$login = ftp_login($connection, $ftp_user_name, $ftp_user_pass);
if (!$connection || !$login) { die(‘Connection attempt failed!’); }
$upload = ftp_put($connection, $dest, $source, $mode);
if (!$upload) { echo ‘FTP upload failed!’; }
ftp_close($connection);
?>

Continue reading

Sending mail from my host/gmail

When I was new to php I was sending mail using php’s basic mail() function, but later I came across phpMailer class, which can be used to send mail using any host of your choice (Gmail, for example).
This is an simple code script needed to send an mail using your host and login details.

<?php
//including phpMailer class
require_once("phpmailer/phpmailer.php");
//creating object for phpMailer class
$mail = new PHPMailer();
IsSMTP();
$mail->SMTPAuth = true;
$mail->From = "Your Email";// example ravid@gmail.com
$mail->FromName = "Your Name";// example Ravi Continue reading