Submit Your Article Forum Rules

Page 1 of 4 123 ... LastLast
Results 1 to 10 of 36

Thread: Apache server question: Configuration and PHP pear question.

  1. #1
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    Question Apache server question: Configuration and PHP pear question.

    I could have looked this up on the internet, but hope for a fast answer here.


    1. I am on a shared server, so I can not use php.ini. Is there a way to give write access to .txt and .xml files in .htaccess. As the server is configured now, I don't have write access to .txt file and XML write to file functions / methods do not function. My hoster will for obvious reasons not give me access to php.ini.
    2. I used pear on my own test server about three years ago. Is there WPW members with pear experience here? If yes:
      - Uploading and configuring experience.
      - Functionality and new versions.

    Comments on any of these topics are welcome.

  2. #2
    WebProWorld MVP wige's Avatar
    Join Date
    Jun 2006
    Posts
    3,138

    Re: Apache server question: Configuration and PHP pear question.

    First, are you absolutely sure that the block is being done in php.ini? It is rare for writes to be blocked from php.ini, especially for .txt files since many users from Windows environments will use the .txt extension on data files created with PHP. I would use a script that displays the php.ini file contents to make sure that is where the block is - more likely you are being blocked by the file system or Apache. That being said, you can't override php.ini settings in .htaccess - PHP can't see anything in the .htaccess files.
    The best way to learn anything, is to question everything.
    WigeDev - Freelance web and software development

  3. #3
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    Re: Apache server question: Configuration and PHP pear question.

    Quote Originally Posted by wige View Post
    First, are you absolutely sure that the block is being done in php.ini?
    No, but I had similar problems some years ago with writing to XML files from a PHP function. I contacted my hoster that told me that I had no access to php.ini. Configuration had to be done on the server.

    Quote Originally Posted by wige View Post
    It is rare for writes to be blocked from php.ini, especially for .txt files since many users from Windows environments will use the .txt extension on data files created with PHP.
    I use three different hosters. I can of course upload the files to the other two and test them there.

    Quote Originally Posted by wige View Post
    I would use a script that displays the php.ini file contents to make sure that is where the block is - more likely you are being blocked by the file system or Apache.
    I may try that.

    Quote Originally Posted by wige View Post
    That being said, you can't override php.ini settings in .htaccess - PHP can't see anything in the .htaccess files.
    I use .htaccess for PHP path settings etc.

    Here is the code that causes problem:

    PHP Code:
    <?php
    /*
     * Copyright 2006 SitePoint Pty. Ltd, www.sitepoint.com
     *
     * Licensed under the Apache License, Version 2.0 (the "License");
     * you may not use this file except in compliance with the License.
     * You may obtain a copy of the License at
     * 
     *   http://www.apache.org/licenses/LICENSE-2.0
     * 
     * Unless required by applicable law or agreed to in writing, software
     * distributed under the License is distributed on an "AS IS" BASIS,
     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     * See the License for the specific language governing permissions and
     * limitations under the License.
     */

    define('NEW_GAME''{"lastMove":{"movePiece": null,"takePieceId":null,"moveTime":null},"pieceList":{"white_r1":{"color":"white","id":"white_r1","pos":[0,0],"origPos":[]},"white_n1":{"color":"white","id":"white_n1","pos":[0,1],"origPos":[]},"white_b1":{"color":"white","id":"white_b1","pos":[0,2],"origPos":[]},"white_q":{"color":"white","id":"white_q","pos":[0,3],"origPos":[]},"white_k":{"color":"white","id":"white_k","pos":[0,4],"origPos":[]},"white_b2":{"color":"white","id":"white_b2","pos":[0,5],"origPos":[]},"white_n2":{"color":"white","id":"white_n2","pos":[0,6],"origPos":[]},"white_r2":{"color":"white","id":"white_r2","pos":[0,7],"origPos":[]},"white_p1":{"color":"white","id":"white_p1","pos":[1,0],"origPos":[]},"white_p2":{"color":"white","id":"white_p2","pos":[1,1],"origPos":[]},"white_p3":{"color":"white","id":"white_p3","pos":[1,2],"origPos":[]},"white_p4":{"color":"white","id":"white_p4","pos":[1,3],"origPos":[]},"white_p5":{"color":"white","id":"white_p5","pos":[1,4],"origPos":[]},"white_p6":{"color":"white","id":"white_p6","pos":[1,5],"origPos":[]},"white_p7":{"color":"white","id":"white_p7","pos":[1,6],"origPos":[]},"white_p8":{"color":"white","id":"white_p8","pos":[1,7],"origPos":[]},"black_r1":{"color":"black","id":"black_r1","pos":[7,0],"origPos":[]},"black_n1":{"color":"black","id":"black_n1","pos":[7,1],"origPos":[]},"black_b1":{"color":"black","id":"black_b1","pos":[7,2],"origPos":[]},"black_q":{"color":"black","id":"black_q","pos":[7,3],"origPos":[]},"black_k":{"color":"black","id":"black_k","pos":[7,4],"origPos":[]},"black_b2":{"color":"black","id":"black_b2","pos":[7,5],"origPos":[]},"black_n2":{"color":"black","id":"black_n2","pos":[7,6],"origPos":[]},"black_r2":{"color":"black","id":"black_r2","pos":[7,7],"origPos":[]},"black_p1":{"color":"black","id":"black_p1","pos":[6,0],"origPos":[]},"black_p2":{"color":"black","id":"black_p2","pos":[6,1],"origPos":[]},"black_p3":{"color":"black","id":"black_p3","pos":[6,2],"origPos":[]},"black_p4":{"color":"black","id":"black_p4","pos":[6,3],"origPos":[]},"black_p5":{"color":"black","id":"black_p5","pos":[6,4],"origPos":[]},"black_p6":{"color":"black","id":"black_p6","pos":[6,5],"origPos":[]},"black_p7":{"color":"black","id":"black_p7","pos":[6,6],"origPos":[]},"black_p8":{"color":"black","id":"black_p8","pos":[6,7],"origPos":[]}}}');

    require_once 
    "JSON.php";

    $json = new Services_JSON();
    $input implode("\r\n"file('php://input'));
    $cmd $json->decode($input);
    $game Game::instance();
    $resp = new Response();
    $out '';

    switch (
    $cmd->cmdName) {
      case 
    'load':
        
    $out '{"respStatus":"ok", "respData":'.$game->getFromFile().'}';
        break;
      case 
    'wipe':
        
    $game->saveToFile(NEW_GAME);
        
    $out '{"respStatus":"ok", "respData":'.NEW_GAME.'}';
        break;
      case 
    'poll':
        
    $lastMove $cmd->cmdData;
        
    $game->load();
        if ((
    $game->state->lastMove->moveTime $lastMove->moveTime)||
          (
    $lastMove->moveTime && !$game->state->lastMove->moveTime)) {
          
    $out '{"respStatus":"update", "respData":'.$game->getFromFile().'}';
        }
        else {
          
    $resp->respStatus 'nochange';
          
    $out $json->encode($resp);
        }
        break;
      case 
    'move':  
        
    $move $cmd->cmdData;
        
    $movePieceId $move->movePiece->id;
        
    $takePieceId $move->takePieceId;
        
    $moveTime date('Y-m-d H:i:s'mktime());
        
    $game->load();
        
    $game->state->pieceList->$movePieceId->pos[0] = $move->movePiece->pos[0];
        
    $game->state->pieceList->$movePieceId->pos[1] = $move->movePiece->pos[1];
        if (
    $takePieceId) {
          unset(
    $game->state->pieceList->$takePieceId);
        }
        
    $game->state->lastMove->movePiece $move->movePiece;
        
    $game->state->lastMove->moveTime $moveTime;
        
    $game->state->lastMove->takePieceId $takePieceId;
        
    $resp->respStatus 'ok';
        
    $resp->respData->lastMove $game->state->lastMove;
        
    $out $json->encode($resp);
        
    $game->save();
        break;
    }
    header('Content-Type: text/plain');
    print 
    $out;

    class 
    Game {
      function 
    Game() {
        
    $this->json = new Services_JSON();
        
    $this->state = new GameState();
        
    $this->filePath 'chessboard.txt';
      }
      function 
    saveToFile($str) {
        if (
    is_writable($this->filePath)) {
          
    file_put_contents($this->filePath$str) or die('Could not write to file '.$this->filePath);
        }
        else {
          die(
    'File is not writable');  //Error occurs here.
        
    }
      }
      function 
    getFromFile() {
        if (
    is_readable($this->filePath)) {
          
    $str file_get_contents($this->filePath) or die('Could not get file contents from '.$this->filePath);
          return 
    $str;
        }
        else {
          die(
    'File '.$this->filePath.' is not readable');
        }
      }
      function 
    load() {
        
    $str $this->getFromFile();
        
    $this->state $this->json->decode($str);
      }
      function 
    save() {
        
    $str $this->json->encode($this->state);
        
    $this->saveToFile($str);
      }
      function 
    instance() {
        static 
    $instance null;
        if(
    $instance === null) {
          
    $instance = new Game();
        }
        return 
    $instance;
      }
    }

    class 
    Response {
      function 
    Response($respStatus ''$respData '') {
        
    $this->respStatus $respStatus;
        
    $this->respData $respData;
      }
    }

    class 
    GameState {
      function 
    GameState($lastMove null$pieceList null) {
        
    $this->lastMove $lastMove;
        
    $this->pieceList $pieceList;
      }
    }

    ?>
    Look for //Error occurs here.

  4. #4
    WebProWorld MVP wige's Avatar
    Join Date
    Jun 2006
    Posts
    3,138

    Re: Apache server question: Configuration and PHP pear question.

    Before the die statement, try adding this line:
    PHP Code:
    print_r(stat($this->filePath)); 
    That should output the owner, group, and active permissions for the file in question.
    The best way to learn anything, is to question everything.
    WigeDev - Freelance web and software development

  5. #5
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    Re: Apache server question: Configuration and PHP pear question.

    Now I got a more meaningful message:

    Array
    (
    [0] => 99
    [1] => 28781981
    [2] => 33188
    [3] => 1
    [4] => 1686
    [5] => 80
    [6] => 115007813
    [7] => 2313
    [8] => 1243010458
    [9] => 1202934269
    [10] => 1202934269
    [11] => 4096
    [12] => 8
    [dev] => 99
    [ino] => 28781981
    [mode] => 33188
    [nlink] => 1
    [uid] => 1686
    [gid] => 80
    [rdev] => 115007813
    [size] => 2313
    [atime] => 1243010458
    [mtime] => 1202934269
    [ctime] => 1202934269
    [blksize] => 4096
    [blocks] => 8
    )
    File is not writable

    Once I saw your proposal I thought

    Before the die statement, try adding this line:

    If that is the only problem, I think it shall be placed higher up in the code?

    A solution is of course to use an array instead of a file, but that is far from the ideal solution and this has to be solved.

    I thought this (the added code) would solve it

    PHP Code:
     function saveToFile($str) {
        
    print_r(stat($this->filePath)); //Added code line
        
    if (is_writable($this->filePath)) {
          
    file_put_contents($this->filePath$str) or die('Could not write to file '.$this->filePath);
        }
        else {
           die(
    'File is not writable');
        }
      } 
    but the code still falls back to the else clause so it gives the same error message as above.

    Thank you so long.

  6. #6
    WebProWorld MVP wige's Avatar
    Join Date
    Jun 2006
    Posts
    3,138

    Re: Apache server question: Configuration and PHP pear question.

    Yeah, execution dropping to the else clause is to be expected, the added code is just to get some information. The line gives us two important pieces of information, that the file belongs to user 1686 and group 80. Next, if you replace the line you added with this line:
    PHP Code:
    echo "File permissions: " sprintf('%o'fileperms($this->filePath)); 
    This should give us the mod code for the file. Also, do you know what the uid and gid for the Apache server are?

    If you can't determine it, have the script create a text file, then run the print_r(stat($this->filePath)); line on that file, which will tell us the uid and gid of the creater (Apache).
    The best way to learn anything, is to question everything.
    WigeDev - Freelance web and software development

  7. #7
    WebProWorld MVP williamc's Avatar
    Join Date
    Jul 2003
    Location
    On a really big hill in Kentucky
    Posts
    4,721

    Re: Apache server question: Configuration and PHP pear question.

    kgun: simply chmod the directory the script creates its files in to 766 OR chown the directory to the apache user.

    That will take care of the issue quickly.
    William Cross
    Web Development by Those Damn Coders
    Firearm Friendly Websites because our constitution matters

  8. #8
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    Re: Apache server question: Configuration and PHP pear question.

    Quote Originally Posted by wige View Post
    Yeah, execution dropping to the else clause is to be expected, the added code is just to get some information.
    Yes I understood that after I have logged out. I was involved in so many discussions. I use the technique myself.

  9. #9
    WebProWorld MVP kgun's Avatar
    Join Date
    May 2005
    Location
    Norway
    Posts
    7,999

    Re: Apache server question: Configuration and PHP pear question.

    Quote Originally Posted by williamc View Post
    kgun: simply chmod the directory the script creates its files in to 766 OR chown the directory to the apache user.

    That will take care of the issue quickly.
    Thank you for a minimalistic answer.


    1. 766 I have seen that elsewhere. Do you remember the exact format?
    2. chown the directory to the apache user Never heard of that before. Can you explain?

    There you got three greens. As soon as you learn to concentrate on posts and not persons you are a MOD candidate IMO.

    Do as I say and not as I do.

    I still miss remarks on the pear package.

  10. #10
    WebProWorld MVP williamc's Avatar
    Join Date
    Jul 2003
    Location
    On a really big hill in Kentucky
    Posts
    4,721

    Re: Apache server question: Configuration and PHP pear question.

    Quote Originally Posted by kgun View Post
    Thank you for a minimalistic answer.


    1. 766 I have seen that elsewhere. Do you remember the exact format?
    2. chown the directory to the apache user Never heard of that before. Can you explain?
    1: chmod 766 /path/to/directory/files/are/created/in
    IE: chmod 766 /home/username/public_html/mydirectoryname

    2: chown changes a file or directories ownership to that of a certain user. at the command line chown --help
    William Cross
    Web Development by Those Damn Coders
    Firearm Friendly Websites because our constitution matters

Page 1 of 4 123 ... LastLast

Similar Threads

  1. Name Server Question
    By watto in forum Search Engine Optimization Forum
    Replies: 14
    Last Post: 11-13-2008, 05:12 PM
  2. Apache question: PHP in HeaderName or ReadmeName?
    By akbigdog in forum Web Programming Discussion Forum
    Replies: 0
    Last Post: 02-26-2005, 06:49 AM
  3. Server Question
    By jmdb71 in forum IT Discussion Forum
    Replies: 10
    Last Post: 12-17-2004, 11:17 PM
  4. Apache question, hiding a part of the url
    By Niko Holopainen in forum Web Programming Discussion Forum
    Replies: 2
    Last Post: 07-28-2004, 05:24 AM
  5. Apache Server Configuration
    By Sualdam in forum IT Discussion Forum
    Replies: 14
    Last Post: 01-20-2004, 09:52 AM

Posting Permissions

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