Script for a web server and CLI

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scuar
Forum Newbie
Posts: 9
Joined: Wed Nov 03, 2010 10:27 pm

Script for a web server and CLI

Post by scuar »

Hello,

i was wondering if is it possible to make a script that is able to be executed from a webserver and from a CLI aswell.
I'm not shure how to check if variables are passed trough an url or by command line.


I've heard that execution time from CLI to regular php execution, is it true?

Scuar.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Re: Script for a web server and CLI

Post by s.dot »

Yes, it is possible.

Although, the CLI php.ini settings file may differ from the web server php.ini settings file.
I'm not sure if this is needed, but in your script, you can check if running from the browser or command line by doing this:

Code: Select all

$cli = isset($_SERVER['argc']) && ($_SERVER['argc'] >= 1);

if ($cli)
{
    //command line
} else
{
    //browser
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
scuar
Forum Newbie
Posts: 9
Joined: Wed Nov 03, 2010 10:27 pm

Re: Script for a web server and CLI

Post by scuar »

Delete this post please
Last edited by scuar on Fri Nov 05, 2010 9:23 am, edited 1 time in total.
scuar
Forum Newbie
Posts: 9
Joined: Wed Nov 03, 2010 10:27 pm

Re: Script for a web server and CLI

Post by scuar »

Great!, it was so simple.

Thanks a lot.

EDIT:
Oops i didn't realize i misspell this question "I've heard that execution time from CLI to regular php execution, is it true?"
What i need to know it's if a cli script can be executed for hours or days (i'm plaining on using sleep() and each hour do something) and if a web script can do it as well.
Post Reply