Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts

    PHP CLI _SERVER help needed

    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...
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,366
    Thanks
    61
    Thanked 530 Times in 517 Posts
    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.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    rfresh (05-01-2011)

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    So I'm using the follow code to open the text file but the data is empty!

    PHP Code:
    $php_handle fopen("http://localhost/domain.txt","r");
    if (
    $php_handle)
       {
         
    $account fgets($php_handle4096);
         
    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?
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #4
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,366
    Thanks
    61
    Thanked 530 Times in 517 Posts
    You need to look up fread on php.net and stream_get_contents (example #3 on fread page)
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • Users who have thanked tangoforce for this post:

    rfresh (05-01-2011)

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    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:
    PHP Code:
    $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:
    PHP Code:
    $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!

    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #6
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    I finally got it to work by taking out the localhost part and just having the file name!

    Thanks for your help.
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,366
    Thanks
    61
    Thanked 530 Times in 517 Posts
    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!
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    My solution below worked for cron scripts, however, for piped scripts it doesn't work 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?

    PHP Code:
    $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.
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,366
    Thanks
    61
    Thanked 530 Times in 517 Posts
    I don't think piping is available on windows.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #10
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    I'm on a Linux server...
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #11
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,096
    Thanks
    11
    Thanked 101 Times in 99 Posts
    is there a problem with just getting the hostname ?
    PHP Code:
    <? 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
    PHP Code:
    file_get_contents('http://localhost/where.php'); 
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #12
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    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.
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,366
    Thanks
    61
    Thanked 530 Times in 517 Posts
    @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...
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #14
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by rfresh View Post
    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:
    Code:
    $ which hostname
    and put the result between backticks as firepage suggested.
    also check if backticks work:

    http://www.php.net/manual/en/languag....execution.php

    best regards
    Last edited by oesxyl; 05-03-2011 at 12:28 AM.

  • #15
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,096
    Thanks
    11
    Thanked 101 Times in 99 Posts
    Quote Originally Posted by tangoforce View Post
    @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
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •