SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    HELP NEEDED on directory of share include file

    //this is my index file on totaljokes-error( a folder name)

    <?php
    include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';

    function totaljokes()
    {
    try
    {
    $result=$pdo->query('SELECT COUNT(*) FROM joke');
    }
    catch (PDOException $e)
    {
    $error='Database error counting jokes!';
    include 'error.html.php';
    exit();
    }

    $row=$result->fetch();
    return $row[0];
    }


    //Even my folder "includes" contained file "error.html.php" and "db.inc.php" in directory wamp/www, but the http://localhost/totaljokes-error/ showed nothing, just blank. Am i wrong placed my file or what else? I'm blur with the share include file in directory.

    Any help is appreciated.

  2. #2
    SitePoint Zealot 2ndmouse's Avatar
    Join Date
    Jan 2007
    Location
    West London
    Posts
    196
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try:
    include_once ($_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php');
    Or use a relative path:
    include_once 'includes/db.inc.php';
    Detect file changes remotely. SimpleSiteAudit is an early
    warning anti-hacker system which sends an alert on detection.

    PHP Find Orphan Files - Finds all the unreferenced files on your site.

  3. #3
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you for willing to help. I had tried both just now, it still didn't work, blank...no such file in directory when i used 'includes/db.inc.php'; and still blank with the parentheses. ~.~...

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    70 Post(s)
    Tagged
    0 Thread(s)
    What's the value of $_SERVER['DOCUMENT_ROOT'] ? You mentioned you were running this on Windows....

    You could try including "./includes/db.inc.php" (forcing a relative reference with .)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i had tried again, same, blank. I just want to know how to use include files in directory. By the way, i followed this php code from PHP & MySQL: Novice to Ninja book, page 177. Help..

  6. #6
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    What's the value of $_SERVER['DOCUMENT_ROOT'] ? You mentioned you were running this on Windows....

    You could try including "./includes/db.inc.php" (forcing a relative reference with .)

    The values appeared is C:/wamp/www/ for $_SERVER['DOCUMENT_ROOT'].
    What I wrote was include_once $_SERVER['DOCUMENT_ROOT'] . '/includes/db.inc.php';
    I had try many times...the browser still go for blank.
    includes folder contained db.inc.php, error.html.php
    totaljokes-error folder contained index.php which i listed up in the first posted.

    //I'm using window to run wampserver,phpmyadmin version 3.4.5

  7. #7
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Jump out of follow the book mode for a short while. Isolate this inability to correctly locate or write your includes from the rest of your book, get to the bottom of the problem, solve it and then go back.

    Try this:

    Create a file called test_include.php

    test_include.php
    PHP Code:
    <?php
    echo 'You have found the test file!';
    ?>
    Then create a file called test.php

    test.php
    PHP Code:
    <?php

    error_reporting
    (E_ALL);
    ini_set("display_errors"1);

    include 
    'test_include.php';

    ?>
    Now put those 2 files in the same folder, load up test.php and tell us if the message 'You have found the test file!' appears.

    If it does, then move the test_include file to your include folder, then keep messing with the path using the suggestions already given above ..
    PHP Code:
    include 'test_include.php';  // add things to this path - hardcode them at first eg /Wamp/www/includes/test_include.php 
    until you get the success message back again.

    Then take a good hard look at the manual page on includes, and decide on how you are going to handle includes initially.

    Come back and ask here if you are unsure what the terminology means, or where the settings are best made. There are many options.

  8. #8
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks to you, Cups. I did tried what you said, its working, after that I inspired by your idea, and i found out that i didn't call the function, that's why my browser blank. The book is correct, just it didn't wrote the code for calling the function, i followed blindly and i thought it was the problem of directory files, actually its a careless mistake. Now i feel happy and proceed with my lesson =D !

  9. #9
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I don't know if it matters but, as an advice, instead of $_SERVER['DOCUMENT_ROOT'] you may use dirname(__FILE__).'/' as a relative reference.
    The document_root may be "far away" from your file so, it's safer to refer to the current file path.
    Good luck!

  10. #10
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what's the difference between dirname(_FILE_) and $_SERVER['DOCUMENT_ROOT']? one more, how to use dirname(_FILE_)? give me an example please, thanks!

  11. #11
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    let's say we have:
    Code:
    index.php
    [lib]
    ---[classes]
    ------class1.php
    ------class2.php
    ---config.php
    To require [root]/lib/classes/class1.php from [root]/lib/config.php we can have:
    1. require dirname(__FILE__).'/classes/class1.php';
    2. require $_SERVER['DOCUMENT_ROOT'].'/lib/classes/class1.php';
    3. require 'classes/class1.php';

    First, 3 is not OK (worst case) because if our "config.php" is required by another file, from another folder, the path will not be valid anymore.
    Let's say that in [root]/lib/classes/class1.php we have require '../config.php'; the final content of the file (class1.php + the include) will be:
    Code:
    <?php
    require '../config.php';
    // -- and this will produce
    require 'classes/class1.php'; // (from our config.php)
    // -- fatal error: we try to require [root]/lib/classes/classes/class1.php - not here!
    1) and 2) use absolute server path so you cannot fail BUT if the website is moved into a folder (it's a strange case, but it can happen - the server is changed and the DOCUMENT_ROOT is not the same or you need a [beta] folder of the website), we'll have:
    Code:
    FOLDER
    ---index.php
    ------[lib]
    ---------[classes]
    ------------class1.php
    ------------class2.php
    ------config.php
    In case (2), instead of
    $_SERVER['DOCUMENT_ROOT'].'/lib/classes/class1.php';
    we'll need
    $_SERVER['DOCUMENT_ROOT'].'/FOLDER/lib/classes/class1.php';

    In the (1)st case everything it's compatible.
    No changes are needed. It's just a better practice that I would suggest.

  12. #12
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i got it for the case(3), but i got problem on case(1). When i tried <?php include_once $_SERVER['DOCUMENT_ROOT'].'/includes/magicquotes.inc.php';, it's working. After that, i tried it with <?php include_once dirname(__FILE__).'/includes/magicquotes.inc.php';, it isn't working. I got an a notice that said "Use of undefined constant _FILE_ ", and 2 warnings which are " include_once(./includes/magicquotes.inc.php) [function.include-once]: failed to open stream: No such file or directory in C:\wamp\www\jokes-helpers\index.php on line 2" and " include_once() [function.include]: Failed opening './includes/magicquotes.inc.php' for inclusion (include_path='.;C:\php\pear') in C:\wamp\www\jokes-helpers\index.php on line 2"

    Anyproblem with dirname(_FILE_) or typing error?

  13. #13
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    use __FILE__ not _FILE_ http://php.net/manual/en/language.co...predefined.php
    Edit: didn't know that __DIR__ appeared since 5.3 so, you may use it if you have PHP 5.3

  14. #14
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Hunge View Post
    Thanks to you, Cups. I did tried what you said, its working, after that I inspired by your idea, and i found out that i didn't call the function, that's why my browser blank. The book is correct, just it didn't wrote the code for calling the function, i followed blindly and i thought it was the problem of directory files, actually its a careless mistake. Now i feel happy and proceed with my lesson =D !
    Thats OK, glad I was able to help.

    Learning from books is really great, but you should get comfortable with giving yourself permission (and time) for diving out of the book and isolating and testing some of the principles for yourself.

    Eventually you will find that development follows the same path, lots of little bits of code that you already know work - joined up together.

    Accepting that writing more code will simply mean you will make more mistakes (at least when starting out) is hard to swallow, but once you start to think about it you can adopt several practices to code more defensively, and your mind should be full of questions like:

    "I just wrote this and that, which means that variable $x should contain 'xxxx' - how can I prove that worked?"

    Some times all it takes is a simple echo or var_dump() (this will be your best friend EVER) and sometimes you should isolate the problem, such as was your case.

  15. #15
    SitePoint Member
    Join Date
    Jan 2013
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Cups View Post
    Thats OK, glad I was able to help.

    Learning from books is really great, but you should get comfortable with giving yourself permission (and time) for diving out of the book and isolating and testing some of the principles for yourself.

    Eventually you will find that development follows the same path, lots of little bits of code that you already know work - joined up together.

    Accepting that writing more code will simply mean you will make more mistakes (at least when starting out) is hard to swallow, but once you start to think about it you can adopt several practices to code more defensively, and your mind should be full of questions like:

    "I just wrote this and that, which means that variable $x should contain 'xxxx' - how can I prove that worked?"

    Some times all it takes is a simple echo or var_dump() (this will be your best friend EVER) and sometimes you should isolate the problem, such as was your case.
    Thanks for your advice. I got it, always think but not follow blindly, haha!


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
  •