SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Guru
    Join Date
    Sep 2004
    Location
    Provo, UT
    Posts
    858
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Correct file paths when using include statements

    This basic script is in a folder off of the root called campaigns. The sponsorLookup.php is a file on the root. The print statement for SponsorFirstName is failing while the fakeSponsor print statement works fine. The only difference between the two included files is that one is on the root and the other is in the campaigns folder. When you want to reference a file that is up one level, you put a / in front of it, right? I've even tried using the full path (http://www.example.com/sponsorLookup.php) and that doesn't work either.

    Thanks!


    PHP Code:
    <?php
    include 'startSession.php';
    $_SESSION[sponsorID] = 121;

    include 
    '/sponsorLookup.php';
    include 
    'fakeSponsor.php';

    print (
    "<p>SponsorFirstName is $_SESSION[sponsorFirstName]</p>");
    print (
    "<p>fakeSponsor is $fakeSponsor");

    ?>
    Convert your dollars into silver coins. www.convert2silver.com

  2. #2
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    If you want to include something from "one level up" you would use "../". If you use "/" then it will look in the root of your servers HDD.

    Also make sure you use quotes when accessing keys in an array. You may want to try and access the sponsorFirstName as such:
    PHP Code:
    print ("<p>SponsorFirstName is {$_SESSION['sponsorFirstName']}</p>"); 
    Note the single quotes around the key name. The curly braces "{}" around the variable aren't always needed, but it helps give PHP a "hint" than the enclosed is a PHP variable.

    Also note that array keys are case sensitive. In your post you refer to the key as SponsorFirstName while in your script you call it as sponsorFirstName.
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  3. #3
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    If you want to include something from "one level up" you would use "../". If you use "/" then it will look in the root of your servers HDD.
    Not quiet. It will look in the Document_Root of your website. Not the root of your HDD.

    start your php script with:
    PHP Code:
    $path '/absolute/path/to/document/root/';
    set_include_path(get_include_path() . PATH_SEPARATOR $path); 
    and do your includes like this:
    PHP Code:
    include('includes/functions.php'); 
    assuming that 'includes' folder is in the root dir from $path

    You might as well do this:
    PHP Code:
    $path '/absolute/path/to/document/root/'.PATH_SEPARATOR.'/absolute/path/to/document/root/includes/';
    set_include_path(get_include_path() . PATH_SEPARATOR $path); 
    and include like this:
    PHP Code:
    include('functions.php'); 
    No more headbreaking ../../blabla/somescript.php or things like that.
    Look in the php manual for the set_include_path() function.
    The Path of excess leeds to the tower of wisdom (W. Blake)

  4. #4
    SitePoint Addict kduv's Avatar
    Join Date
    May 2012
    Location
    Maui, HI
    Posts
    211
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    So what would happen if they included their files like "/somefile.php"?
    Keith
    Freelance web developer
    http://www.duvalltech.com/

  5. #5
    SitePoint Evangelist pompopom's Avatar
    Join Date
    Feb 2004
    Location
    Huldenberg (Belgium)
    Posts
    426
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kduv View Post
    So what would happen if they included their files like "/somefile.php"?
    That depends on how the include path is set. With or without trailing /
    The Path of excess leeds to the tower of wisdom (W. Blake)


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
  •