Archive for the “PHP” Category

PHP: Running PHP Script with Cron

This is not my personal article but thought it was worth having on here : 

reference : http://www.htmlcenter.com/blog/running-php-scripts-with-cron/

Lots of programmers like PHP for its ability to code and develop web applicationsfast. Code-debugging is a lot easier than with PERL or C. However, there is one thing a lot of developers are puzzled about, “How to run PHP Scripts with crontab?”

Cron is normally available on all Unix and Linux distributions; if you cannot access it, contact your root or server administrator. It is a daemon which allows you to schedule a program or script for a specific time of execution. If you want to learn more about cron, click here or type “man crontab” at your command prompt.

I have found myself in the need to run PHP scripts at specific times. For example, to update the content of a website, to remove expired articles, to send out e-mails on a given date and a lot more. While some may think that this is were PHP is doomed, I will show you how it’s done.

A Manual crontab?

The first solution that came to my mind was to run the script directly from my browser (e.g. http://www.mydomain.com/script.php). Since I need to run my script on a regular basis, I squashed that idea. My goodness, all the extra hassle is ridiculous.

 

An include?

Another possible solution is to include the script in one of the pages of the site, for example the very first: “index.php”. (<? include “cron.php”; ?>)

The drawbacks to this solution are, that it works but when someone accesses the “index.php”. This could cause a lot of extra overhead produced by the script. If you get a lot of traffic, the script is executed 1000 times a day and adds a lot of usage on the database and the server.
On the other hand, if you do not get a lot of traffic, or people tend to access your site over another file, this will not work out as well. If you need to run the script on a regular intervals, this is not a solution.

Crontab!

Let’s suppose you either know what cron is or have read about it using the link above. We want to run our script once a minute. So where do we go from here? Here is how you can accomplish this task.

Your PHP setup

You will need to find out the answer to the following question, “Is my PHP installed as CGI or as an Apache module?”. To find out do the following: Create a new file, name it info.php (just an example), and put in the following code, “<? phpinfo(); ?>”. Upload to your webserver and go to it with your browser.

Now check for Server API (4th item from the top), if it says “CGI”, you have PHP compiled as CGI, if it reads “Apache”, you have it running as an Apache module.

Compiled CGI

If the answer to the question above is “CGI” then you need to add a line to your PHP script. It has to be the first line of your script and must contain your server’s PHP executable location:

#!/usr/local/bin/php -q

That looks a lot like PERL now, doesn’t it? After that let’s add the necessary command to our crontab. Edit /etc/crontab and add the following line:

* * * * * php /path/to/your/cron.php

Execute the following from the command line:

Shell&gt; crontab crontab

Be sure your “script.php” has the necessary permissions to be executable (”chmod 755 script.php”).

Now you are all set!

Apache module

If your PHP is installed using the Apache module, the approach is a little different. First, you need access to Lynx (Lynx Browser for more information). Lynx is a small web browser, generally available on Unix and Linux.

Running your PHP script will not require you to add any additional lines. You simply have to edit your /etc/crontab file and add the following line:

* * * * * lynx -dump http://www.somedomain.com/cron.php

Please note that in general, you have to specify the entire URL (with “http://” and so on). But depending on your Lynx’s configuration, the URL might be relative; I suggest always using the absolute reference as in my example above – it always works.

Again execute the following from the command line:

Shell&gt; crontab crontab

That all it takes to get a cron job setup using PHP. Hope you have learned something new and will use it to save overhead time on the server and on the

=====additional notes ===
some scripts are setup in a way that they need to be ran via http

0 * * * * wget -O – -q -t 1 http://www.example.com/cron.php

May 4, 2009 Posted Under: PHP   Read More

PHP: CGI Mode

forcing PHP CGI Mode under your domain name.

Compile PHP without the apache otpions and cp the PHP-cgi file to your cgi-bin . Then create a htaccess file with
Action php-script /cgi-bin/php-cgi
AddHandler php-script .php

Plesk *rememeber to chown the binary to user:psacln * replace user with your username name.

Thanks
How to compile without the apache options

March 2, 2009 Posted Under: PHP   Read More

PHP: installing magicwand

** REQUIRED ** ImageMagick version :: 6.3.5-9
Last-known-good ImageMagick version :: 6.3.5-9

1.) Check out the latest version from magicwand servers

svn co https://www.imagemagick.org/subversion/MagickWandForPHP/branches/MagickWandForPHP-1.0.7 MagickWandForPHP-1.0.7

I would recommend going to http://magickwand.org/ to verify the latest version , at the time of the post the latest was 1.0.7

2.) untar the compressed file using tar xvzf filename.tar.gz

3.) cd into the new folder and run phpize
4.) run ./configure
5.) make
6.) make test
7.) make install
8.) make clean

note * on cpanel the make install will install the extension in /usr/local/lib/php/extensions/no-debug-non-zts-20060613/ *
for all other distros you will need to move the magickwand.so to your extension folder. you can locate your current ext_dir by issuing the following command php-config –extension-dir

or added the following to your php.ini file
9) add extension=magickwand.so ** you may need to note the dir location of the module such as =/folder/magickwand.so
Magicwand

January 7, 2009 Posted Under: CPANEL, PHP   Read More

PHP: Notice: Undefined variable: HTTP_SESSION_VARS in

HTTP_SESSION_VARS is part of older PHP version and it is recommended not to use it

Undefined variable: HTTP_SESSION_VARS in

to clear turn register long arrays on

register_long_arrays = On
Pre defined variabled

Session Register

January 2, 2009 Posted Under: PHP   Read More

wordpress: update failed , unable to log in

after updating wordpres , you are unable to log in. Try renaming the plugins folder to something like plugins1 . then try to log in. after logging in , rename the plugins folder back and reactivate them. :)

November 29, 2008 Posted Under: PHP   Read More