Welcome to the JaguarPC Community
JaguarPC
Sales: (888) 338-5261
Support: (888)-551-3050
Results 1 to 4 of 4

This is a discussion on Interesting problem with renaming... in the Shared & Semi-Dedicated forum
Ok, I have a simple cgi program that I'd like to be able to "turn on and off" with a php script. Easy enough, just ...

  1. #1
    JPC Guru
    Join Date
    Jan 2004
    Location
    I'm right behind you....
    Posts
    389

    Interesting problem with renaming...

    Ok, I have a simple cgi program that I'd like to be able to "turn on and off" with a php script. Easy enough, just rename it and it'll be turned off! So, I use:

    Code:
    <?php
    rename("on.cgi", "off.cgi");
    ?>
    That should do nicely, right? Wrong! Sadly, permission is denied. Well, of course permission was denied; the directory is not writeable by others. The script works just fine if you chmod 777 the directory, but who in their right mind would leave a directory with cgi files like that?! Even if I were that crazy, the script won't run if it's writeable by others anyway. So, that's not even an imaginary option.

    So, my question: Is there some way around this? Something I can put in the script to sort of "authorize it" to rename the file?

  2. #2
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,306
    Hmmm wouldn't your approach be throwing errors everytime something tried to run on.cgi while it was named off.cgi? I know that this would accomplish your goal but still....

    1) If you have the ability to modify on.cgi then an easy way might be to have an authorization file -- that is a file named onOrOff.txt that is chmodded to 777. Then your on/off script could simply

    print "on" > onOrOff.txt

    or

    print "off" > onOrOff.txt

    as appropriate. Have your on.cgi script read this file to govern itself accordingly (ie simply exit if the file contains "off").

    2) You could even use the existance or not of a file in another directory to govern further execution. I forget the syntax, but the php equivalent of "If -d file == 0" .....

    EDIT: I looked it up for you...
    PHP Code:
    <?php
    $filename 
    '/path/to/foo.txt';

    if (
    file_exists($filename)) {
       echo 
    "The file $filename exists";
    } else {
       echo 
    "The file $filename does not exist";
    }
    ?>
    Source: http://us2.php.net/file_exists

    3) Another thought I had, but have no idea if it will work is have the on.cgi script linked to another file that is writable -- and alter the contents of the secondary file doubt it would work, but you could try it if you're constrained to your methodology.

    Good luck
    Last edited by Ron; 01-21-2005 at 03:57 AM.

  3. #3
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    6,003
    On some of the servers, cgi scripts run under your user id (but PHP scripts do not). If this is the case for you, a cgi script could rename the other cgi. Even if your server isn't set up this way, you can probably put your togle script in a directory called scgi-bin and have it run under your username and force it to run from your UID that way. You can even make a PHP script run in cgi mode by adding a "shebang" line to it and changing the extension to .cgi--just add #!/usr/bin/php to the first line of your script (even before the <? or <?php).

    That said, I still like Ron's idea of having an onoff.txt file better. That way it wouldn't break links when the script was in its "off" state. With that method you would only need to keep a single file CHMODed to 666. With the checking for the existance of a file method you would need to keep a directory set to 777 so that the script could both look for and create the file (you could get around this by logging in via ssh and 'touch'ing the on/off file when you needed to, but it sounds like you want to automate the process). Also, just renaming the file could allow someone to still run the script if they could igure out the "off" name, whereas placing a trap in the code would be much more difficult to get around.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  4. #4
    Ron
    Ron is offline
    Loyal Client
    Join Date
    Aug 2002
    Posts
    7,306
    You're welcome, Mr. Walking Ego....

Bookmarks

Posting Permissions

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