read and modify a text file from 2 different hosted sites

Discussion in 'PHP' started by gio, Sep 20, 2010.

  1. #1
    is this possible? for example i have a text file (sample.txt) hosted at webhosting A. i want this file to be accessed and modified by site1(hosted at webhosting B) and site2(hosted at webhosting C).

    How do i do this?

    Thanks for any input.
     
    gio, Sep 20, 2010 IP
  2. Kakers

    Kakers Active Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #2
    You should be able to do this, but you might have to have some form of connection between the servers such as SSH. If you are then opening the servers to SSH you want to restrict access to it by iptables and authorized keys. However this option is only really open to dedicated hosts or if you host the servers yourself, a lot of companies that only sell web hosting will not provide facilities such as SSH without going dedicated.
     
    Kakers, Sep 20, 2010 IP
  3. gio

    gio Well-Known Member

    Messages:
    2,390
    Likes Received:
    162
    Best Answers:
    0
    Trophy Points:
    195
    #3

    assuming i have SSH access from the 3 hosting i have, how can i implement what i want using PHP?
     
    gio, Sep 20, 2010 IP
  4. axelay

    axelay Peon

    Messages:
    54
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,

    I think the best way of achieving this would be to implement a REST method on the server where the file is held.
    If you want to add to the sample.txt on server A, I suggest you create a PHP script that respond to a form. Then from server B, you call that script with curl and pass whatever info you want to add to the text file. That way server A will get the request, open the file and append to it efficiently. You could also make sure server B is calling server A by checking the IP or the referrer.
    There are many ways of doing it, but I doubt any server will let you modify a text file remotely as this is a huge security risk

    aXe
     
    axelay, Sep 21, 2010 IP
  5. Kakers

    Kakers Active Member

    Messages:
    11
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #5
    I only know how to do this for a Unix OS though.

    
    <?php
    $output1 = shell_exec("shell command here");
    $output2 = shell_exec("shell command here");
    echo $output1;
    echo $output2;
    ?> 
    
    PHP:
    Shell command you want is:

    
    rsync -Pv <location of file to move> <account>@<ip address>:<file location>
    
    Code (markup):
    So an example would be...
    rsync -Pv /usr/bin/script.sh
    That would move the file script.sh from the host server to the same location on the 192.168.0.1 server using the root account.

    Hope this helps.
     
    Kakers, Sep 22, 2010 IP
  6. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #6
    The nature of the alterations can make a difference, but in a nutshell;

    You would use the SSH2 functions to create a lock file, check the creation time of all lockfiles present, if the scripts lockfile is the oldest it makes updates to the file, then deletes it's own lockfile. If it doesn't have the oldest lockfile it immediately deletes it's own lockfile and tries again in N seconds.
     
    joebert, Sep 22, 2010 IP
  7. gio

    gio Well-Known Member

    Messages:
    2,390
    Likes Received:
    162
    Best Answers:
    0
    Trophy Points:
    195
    #7
    thanks for all your replies! :) i finally found a way using just php and just two different hosting. i actually don't need a third one. i tried SSH2 but i think you need a root access to do that.
     
    gio, Sep 22, 2010 IP
  8. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #8
    Ah. I was under the impression you had multiple remote hosts that needed to update the file.
     
    joebert, Sep 22, 2010 IP
  9. gio

    gio Well-Known Member

    Messages:
    2,390
    Likes Received:
    162
    Best Answers:
    0
    Trophy Points:
    195
    #9
    yeah that was my original idea but i realized it can be done with only 2 remote hosts
     
    gio, Sep 22, 2010 IP