...

View Full Version : PHP CLI _SERVER help needed



rfresh
05-01-2011, 08:05 AM
I need to get the name of the domain my PHP cron script is running on. Normally I use $_SERVER[SERVER_NAME] but that doesn't work when running a cron job under CLI. Is there a command I can use in CLI to get the domain name?

Thanks...

tangoforce
05-01-2011, 02:13 PM
CLI = command line interface. It's a feature which allows php to be used in a non web dev environment (EG you can write complex php scripts to do jobs which may not be possible with batch files).

In your situation the $_SERVER['SERVER_NAME'] will not be available because when running as CLI, php isn't aware of any web server environment and since there was no calling web browser, it will not be able to see a domain name where you could normaly find it in the _SERVER array.

The only thing you can do in this instance is to have a text file on the disk with the server name in it. Thats not going to help you much though if you have multiple domains on the same hosting account. The only other thing you could do is call the script from an external source as a http request via its domain name. There are several web based cron services out there or you could use your own windows scheduler to call the site.

rfresh
05-01-2011, 08:54 PM
So I'm using the follow code to open the text file but the data is empty!



$php_handle = fopen("http://localhost/domain.txt","r");
if ($php_handle)
{
$account = fgets($php_handle, 4096);
fclose($php_handle);

$fdw = fopen("/home/account/public_html/logs/text.txt", "a");
fwrite($fdw, "fopen success:".$account."\n");//<--- $account is empty
fclose($fdw);
}


The file to verify the code is working above, the fwrite success doesn't show the $account data which is simply the domain name.

The fopen is working but apparently not the fgets?

tangoforce
05-01-2011, 10:40 PM
You need to look up fread on php.net and stream_get_contents (example #3 on fread page)

rfresh
05-01-2011, 11:42 PM
The examples given all use domain names and I don't have access to the domain name because I'm running my script CLI.

Thus I can't do this as the examples show:


$stream = fopen("http://website.com/domain.txt", "r");
echo stream_get_contents($stream);
fclose($stream);


So I tried using localhost but that didn't work:


$stream = fopen("http://localhost/domain.txt", "r");
echo stream_get_contents($stream);
fclose($stream);


I can't believe I am having so much trouble opening a file in CLI!

:confused:

rfresh
05-01-2011, 11:57 PM
I finally got it to work by taking out the localhost part and just having the file name!

Thanks for your help.

tangoforce
05-02-2011, 12:27 AM
Well that was confusing.. you were trying to access a file via http at localhost and then saying that you can't access files via http because they use a domain :~

Glad you got it working though!

rfresh
05-02-2011, 07:29 AM
My solution below worked for cron scripts, however, for piped scripts it doesn't work :confused: I have a forwarding file setup for a specific email address coming into my website. When the email comes in, I pipe it to one of my php scripts and the code below, which worked for cron jobs, doesn't work.

Must be something different about piped scripts? Are they considered CLI scripts or no?



$stream = fopen("domain.txt", "r");
$account = stream_get_contents($stream);
fclose($stream);


For my piped script, $account var is empty and should contain some text.

tangoforce
05-02-2011, 11:53 AM
I don't think piping is available on windows.

rfresh
05-02-2011, 02:50 PM
I'm on a Linux server...

firepages
05-02-2011, 02:56 PM
is there a problem with just getting the hostname ?


<? echo `hostname`; //note those are backticks ?>


... or for virtual hosting just add a page to the domain e.g. where.php
<?=$_SERVER['SERVER_NAME'];?> and then


file_get_contents('http://localhost/where.php');

rfresh
05-02-2011, 05:28 PM
Yes there is a problem because the script runs as a CLI and not as an HTTP called script, therefore those don't work.

I found the solution for a piped script: I added an argument in the mail forwarding setup calling the script. So now each of the 3 websites that end up calling this script is using it's unique arg[1] so I can identify which website it is running on.

tangoforce
05-02-2011, 11:15 PM
@firepages: I take it your website is firepages.com.au? - I liked the article for the USb wamp many moons ago which you still have. Nice.

When are you going to get the rest of your site fixed up and sort the broken links? Your site has many inspirational articles yet most of the site is dead and when i joined your forum years ago i was unable to post because there was no diskspace left!

I tried contacting you but email bounced, form mail bounced...

oesxyl
05-03-2011, 12:14 AM
Yes there is a problem because the script runs as a CLI and not as an HTTP called script, therefore those don't work.
did you tried? i bet it work, :)

[edit]because you use this in a cron you need to use absolute path to the scripts. So type in a terminal:


$ which hostname

and put the result between backticks as firepage suggested.
also check if backticks work:

http://www.php.net/manual/en/language.operators.execution.php

best regards

firepages
05-03-2011, 03:20 PM
@firepages:
When are you going to get the rest of your site fixed up and sort the broken links? .......
I tried contacting you but email bounced, form mail bounced...

:) I know terrible isn't it , I did start a revamp of the site locally a month or so ago but I never ever have the time to finish anything anymore :(

I have promised myself that when I have finished the main project I am on now that I am going to take time out to get the site up to scratch... not the first time I have promised myself though, but thanks for the heads up!

hostname will work if exec()'d or backticks as cli but it will only give the hostname of the server not the individual website, either way looks like you have it sorted :)

tangoforce
05-03-2011, 03:36 PM
:) I know terrible isn't it , I did start a revamp of the site locally a month or so ago but I never ever have the time to finish anything anymore :(

I have promised myself that when I have finished the main project I am on now that I am going to take time out to get the site up to scratch... not the first time I have promised myself though, but thanks for the heads up!


Seriously, it's nice to see you're alive because your site has been so broken for so long that i'd lost hope. I've seen you on a few other forums and tried to contact you but that seemed unsuccessful too.

You have some great articles and ideas there. Honestly firepages, you have no idea how inspirational your site can be. After seeing your usb for apache 2 i spent a lot of time back porting the idea for apache 1.3 which at the time was still fairly mainstream. I never managed to get the directory sizes as small as yours but once finished that setup served me well for the last 5 years, ran on numerous machines, usb drives, i think i even had it on a mobile phone at one point so it could run off that via usb.

Great site, great imagery it just needs its great webmaster back (with contact details!)

firepages
05-03-2011, 03:58 PM
aghhhhh flattered into doing some work :) , appreciated.

tangoforce
05-03-2011, 04:36 PM
Well years ago i did try to contact you to say the usb thing was a great idea. contact@ bounced and your form did nothing but email itself to me!

5 years of interest = flattery :D

Seriously, nice site :thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum