Variable Won't Send

Discussion in 'PHP' started by dp-user-1, May 3, 2007.

  1. #1
    I have an include file that generates a few things based on a variable called $pageid (such as page title, heading, menu button, etc.) It's very handy, as it dynamically important portions of every page.

    Anyway, I've recently added another folder to my site, which is protected via .htaccess, and the include files don't seem to be noticing the variable coming from the new folder.

    I had to use this line of code just to get the new page to call on the include file:
    ini_set("include_path", "/includes");
    PHP:
    Any ideas why $pageid isn't making it to the include file? :confused:

    Thanks,
    Peter
     
    dp-user-1, May 3, 2007 IP
  2. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Anybody have any ideas?
     
    dp-user-1, May 3, 2007 IP
  3. Spikeon

    Spikeon Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    honestly, there isn't enough code for me to tell you much of anything...
     
    Spikeon, May 3, 2007 IP
  4. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #4
    Sorry, here...

    Average, working page on the site. These pages are in the root directory, as is the include folder. The filepath is root/workingfile.php:
    <?php
    $pageid = "clients";
    include "includes/header.inc.php";
    ?>
    PHP:
    The code to a page in the protected folder. The filepath is root/protected/index.php. This file does not display the $pageid variable like the ones in the root do, however I receive no error message:
    <?php
    $pageid = "protected";
    ini_set("include_path", "/includes");
    include "http://www.monochromedia.com/includes/header.inc.php";
    ?>
    PHP:
    One of the include files. The filepath is root/includes/header.inc.php. I have included code snippets (shown below) so you can see the function of the $pageid variable:
    <title>Monochromedia - Premium Web and Graphic Design | <?php
    if ($pageid != "faq"){
    $pageid = ucfirst($pageid);
    }
    else if ($pageid == "faq"){
    $pageid = "FAQ";
    }
    echo $pageid;
    $pageid = strtolower($pageid);
    ?></title>
    PHP:
    <body id="<?php echo $pageid;?>">
    <div id="container">
    	<div>
    	<a href="index.html"><img src="http://www.monochromedia.com/images/logo.gif" alt="" /></a>
    	</div>
    <div id="spacer">
    	<div id="main">
    		<div id="top">
    			<h1><?php echo $pageid;?></h1>
    PHP:
     
    dp-user-1, May 3, 2007 IP
  5. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #5
    Try using

    global $pageid;

    on top of header.inc.php
     
    designcode, May 3, 2007 IP
  6. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #6
    I'd rather not use global variables.
     
    dp-user-1, May 4, 2007 IP
  7. nabil_kadimi

    nabil_kadimi Well-Known Member

    Messages:
    1,065
    Likes Received:
    69
    Best Answers:
    0
    Trophy Points:
    195
    #7
    Try to add 'echo's too see if all you code is reached
     
    nabil_kadimi, May 4, 2007 IP
  8. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #8
    That's the whole problem. The variable isn't sending.
     
    dp-user-1, May 4, 2007 IP
  9. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #9
    Until I can figure this out I'll just use this:
    if  ( empty($pageid) ){
    	$pageid = "protected";
    }
    PHP:
     
    dp-user-1, May 5, 2007 IP
  10. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #10
    I assume you mean the problem is with this snippet of code:
    <?php
    $pageid = "protected";
    ini_set("include_path", "/includes");
    include "http://www.monochromedia.com/includes/header.inc.php";
    ?>
    PHP:
    where header.inc.php cannot access the $pageid variable.
    1) You do not need to set the include_path to go up a directory.
    2) If you try and include a file using a URL wrapper, the include_path becomes irrelevant.
    3) In most cases, including using a URL wrapper will only return the output of the included file - i.e. the script is executed and then that output is send back to the script from which it was called. For obvious security (and slightly less significant, load) issues, you don't want to use a URL and you definitely don't want your server to allow scripts included in this way to inherit the variable scope. Use a relative or absolute path, not a URL.

    So that should become:
    <?php
    $pageid = "protected";
    include '../includes/header.inc.php';
    ?>
    PHP:
     
    rodney88, May 5, 2007 IP
  11. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #11
    Warning: main(../includes/header.inc.php) [function.main]: failed to open stream: No such file or directory in /home/username/public_html/protected/folder/file.html on line 3
    Code (markup):
    Same goes for all included files...
     
    dp-user-1, May 5, 2007 IP
  12. lemaitre

    lemaitre Peon

    Messages:
    61
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Given the error message you got, your include should read

    <?php
    $pageid = "protected";
    include '../../includes/header.inc.php';
    ?>
    
    PHP:
    Since the relative paths are causing confusion, you could also use:

    <?php
    $pageid = "protected";
    include '/home/username/public_html/includes/header.inc.php';
    ?>
    
    PHP:
     
    lemaitre, May 5, 2007 IP
  13. dp-user-1

    dp-user-1 Well-Known Member

    Messages:
    794
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #13
    :) :) :)

    lemaitre, your first example didn't work, however the second went off without a hitch.

    Thanks for the help!
     
    dp-user-1, May 5, 2007 IP