Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13
  1. #1
    New Coder
    Join Date
    Jan 2013
    Location
    Texas, USA
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Code from PHP include not pulling in html

    I have a code to pull inactive users from the database, and show it up in a table. If i go to the php file (getunregusers.php) it shows up just fine, but using include, include_once, or require in either the head of the html or the body where i would want it to show isn't working at all and cuts off the rest of the script.

    To add, I have a different php file to check login security for pages, and it's includes to connect to the database are the exact same as getunregusers.php and the security works fine, so I don't see any issues with the path to the files included/required.

    getunregusers.php:
    PHP Code:
    <?php
    function getOpenUsers()
    {
        
    // load required files
        
    require('config/dbconfig.php');
        include(
    'classes/class.mysqli.php');
            
        
    // connect to the database
        
    $db = new db("mysql:host=$db_host;dbname=$db_name"$db_user$db_pass);
        
    $db->setErrorCallbackFunction("errorHandler");

        
    //query database
        
    $results $db->select('users');
        
    //check results
        
    if (count($results) > 0)
        {
            
    $rowArray '<table>';
            
            foreach(
    $results as $row)
            {
                if (
    $row['regcode'] != '')
                {
                     
    $rowArray .= '<tr><td>';
                     
    $rowArray .= $row['email'];
                     
    $rowArray .= '</td><td>';
                     
    $rowArray .= $row['regcodenon'];
                     
    $rowArray .= '</td>/<tr>';
                }
            }
            
    $rowArray .= '</table>';
            return 
    $rowArray;
        }
    }
    ?>
    Pulling it up in a different php file in html:
    PHP Code:
    ..section start, article start, and a form above
    <?php
                        
    include_once('classes/class.getunregusers.php');
                        echo 
    getOpenUsers();
                    
    ?>
                </article>
            </section>
    Like I said, ive used inclue, include_once, and require in the body and head. I can post the entire html page if needed to see if something is stopping it, but the only other thing really in there is an include to check sessions set and an id in the database. There is a header to redirect as well but that shouldn't be stopping it as the 2nd posted code doesn't even allow html to continue.

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by gkjr1 View Post
    If i go to the php file (getunregusers.php) it shows up just fine, but using include, include_once, or require in either the head of the html or the body where i would want it to show isn't working at all and cuts off the rest of the script.

    getunregusers.php:
    PHP Code:
    <?php
        
    require('config/dbconfig.php');
        include(
    'classes/class.mysqli.php');
    Pulling it up in a different php file in html:
    PHP Code:
                include_once('classes/class.getunregusers.php'); 
    I think you'll find thats your problem

    When you include one script inside another, it's current working directory is that of the including script. In other words the script with the header is where the current working directory is. When you include a script in that, using file paths for its own includes from its own CWD, the CWD will be wrong because it is not running as its own script in its own CWD - it's running as part of another script in a different CWD.

    You need to turn on error reporting in your code to be seeing these helpful hints and tips.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #3
    New Coder
    Join Date
    Jan 2013
    Location
    Texas, USA
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    I think you'll find thats your problem

    When you include one script inside another, it's current working directory is that of the including script. In other words the script with the header is where the current working directory is. When you include a script in that, using file paths for its own includes from its own CWD, the CWD will be wrong because it is not running as its own script in its own CWD - it's running as part of another script in a different CWD.

    You need to turn on error reporting in your code to be seeing these helpful hints and tips.
    That's kind of what I was explaining. Another of my for the user security on the same page uses those same paths, and getunregusers.php file is in the same folder as the usersecurity.php being included on the html file.

    this is included in a file in the root (the file with html including getunregusers.php)
    PHP Code:
    include(classes/class.getunregusers.php
    getunregusers.php code is now in the root directory. So the includes in getunregusers.php are the correct path.

    PHP Code:
    require('config/dbconfig.php'); 
    include(
    'classes/class.mysqli.php'); 
    Code:
    ->Root
       php file with html
       ->classes folder
       ->config folder
    Correct?

  • #4
    Senior Coder
    Join Date
    Apr 2011
    Location
    London, England
    Posts
    2,120
    Thanks
    15
    Thanked 354 Times in 353 Posts
    Personally, I tend to take a simplistic approach to PHP paths as they are more complicated than first appears. I would try

    'thisfolder/thispage.php'

    then

    '../thisfolder/this.page.php' // go up a level

    then

    '/thisfolder/thispage.php' // start at root

    but the root isn't always where we expect! So

    PHP Code:
    <?php 
       $path 
    $_SERVER['DOCUMENT_ROOT'];
       
    $path .= "/common/header.php";
       include_once(
    $path);
    ?>
    from this page

    I know that this is not very scientific but it works for me most of the time

    If your current situation is more complex (one file including another file, with another include) then getting a reference to DOCUMENT_ROOT will help.
    "I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
    Validate your HTML and CSS

  • #5
    New Coder
    Join Date
    Jan 2013
    Location
    Texas, USA
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    I appreciate the root path code, but I'm trying to say that is not the answer or issue of this problem.

    Directory
    Code:
    ->Root
       example.php
       ->classes folder
          class.pagesecurity.php
          class.getunregusers.php
       ->config folder
          dbconfig.php
    I have file:
    PHP Code:
    classes/class.pagesecurity.php 
    In that file is this:
    PHP Code:
    require('config/dbconfig.php');  
    include(
    'classes/class.mysqli.php'); 
    class.pagesecurity.php is included in example.php as so:
    PHP Code:
    include(classes/class.pagesecurity.php
    It works perfectly fine.

    Now....
    The file with the issue:
    PHP Code:
    classes/class.getunregusers.php 
    In that file is this:
    PHP Code:
    require('config/dbconfig.php');  
    include(
    'classes/class.mysqli.php'); 
    class.getunregusers.php is also included in example.php as so:
    PHP Code:
    include(classes/class.getunregusers.php
    But it doesn't work and cuts off all the html under the include.

    It has the same files to include, and it's at the same location, and it's being included in the same php file with html. The path isn't the problem, like I've been saying...

    There is some other issue and I can't seem to figure it out, being that class.getunregusers.php works just find if i change it's includes and go directly to the file, it's when I include it in example.php, and set the correct paths, that it cuts it off.
    Last edited by gkjr1; 01-28-2013 at 12:47 AM.

  • #6
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by gkjr1 View Post
    But it doesn't work and cuts off all the html under the include.

    It has the same files to include, and it's at the same location, and it's being included in the same php file with html. The path isn't the problem, like I've been saying...
    Have you turned on error reporting yet and if you have, what error messages are present?
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #7
    New Coder
    Join Date
    Jan 2013
    Location
    Texas, USA
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Have you turned on error reporting yet and if you have, what error messages are present?
    I used this in a blank php file:
    PHP Code:
    <?php
      error_reporting
    (E_ALL);
      
    ini_set("display_errors"1);
      include(
    "classes/class.getunregusers.php");
     
    ?>

    Correction! It outputs the results. My test file is also in the same folder as the example.php im trying to make this work in... I have no idea why it won't work in the example.php file. :/

    This is the output:

    Code:
    emailhidden@yahoo.com 1234567891234567 / 
    
    emailhidden@yahoo.com 1234567891234567 /
    Dunno why the "/" are there...
    Last edited by gkjr1; 01-28-2013 at 02:04 AM.

  • #8
    New Coder
    Join Date
    Jan 2013
    Location
    Texas, USA
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Um, never mind. Redefined the database object.....


    Sorry guys.

  • #9
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by gkjr1 View Post
    I used this in a blank php file:

    Correction! It outputs the results.
    Correction to what? - All you've said is you put the code in a blank php file

    Please, stop talking in riddles and ignoring my advice.

    Turn on error reporting within your actual script. It is pointless, absolutely pointless to do it within a blank php file and then tell us those results.

    I feel like I am chasing my own tail in circles..
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #10
    New Coder
    Join Date
    Jan 2013
    Location
    Texas, USA
    Posts
    35
    Thanks
    5
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by tangoforce View Post
    Correction to what? - All you've said is you put the code in a blank php file

    Please, stop talking in riddles and ignoring my advice.

    Turn on error reporting within your actual script. It is pointless, absolutely pointless to do it within a blank php file and then tell us those results.

    I feel like I am chasing my own tail in circles..
    Ummmmmmmm........................
    ->
    Quote Originally Posted by gkjr1 View Post
    Um, never mind. Redefined the database object.....


    Sorry guys.

    I wasn't talking in riddles. I accidently deleted the "It's not showing anything" for errors part. Dont' be a jerk it's unnecessary really. You probably feel like your chasing your own tail in circles because you are. I posted never mind, and the solution, because the problem is solved. There really wasn't a need for the last post whatsoever, so yes, you are chasing your tail in circles but it is your own fault. If you don't understand and actually want clarification, asking more kindly is usually a good way to not upset people in a community, unless you're one who just doesn't care to do that, so why are you trying to help anyone out in the first place.

    I posted that errors were blank and the entire page was blank period on a test page. I did it on a test page to isolate it first. Call me anal but that's what I do is start from 0 and I wanted to make sure again I was getting out the info I wanted. I realized I hadn't called the function on that blank page to show it up, thus, the "correction" part. Sorry I didn't mean to delete the first part of that sentence. It was an accident and doesn't deserve any rude replies ya know? Anyways, see how meaningless all that information was? That's why it wasn't important and left out instead, as you call a "riddle".

    And talk about riddles (really? riddles) and chasing tails.....

    Quote Originally Posted by tangoforce View Post
    I think you'll find thats your problem

    When you include one script inside another, it's current working directory is that of the including script. In other words the script with the header is where the current working directory is. When you include a script in that, using file paths for its own includes from its own CWD, the CWD will be wrong because it is not running as its own script in its own CWD - it's running as part of another script in a different CWD.

    You need to turn on error reporting in your code to be seeing these helpful hints and tips.
    If you would have paid more attention to my op you should have noticed that the directories were in fact correct.

    I'm not trying to be a jerk, but as advice maybe more kind replies would be benefitial to everyone? I do appreciate the attempts for help.

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by gkjr1 View Post
    Ummmmmmmm........................
    ->
    If you notice, I posted my reply at 5 minutes after you apparently posted that.

    Now it took me around 12 minutes to actually write the reply before I hit "Submit Reply" as I was struggling to try and understand what you were actually getting at. Therefore when I had written my reply and finished it, as far as I knew, your final reply did not exist. In other words, I'd already hit reply 7 minutes before you posted your final "Solved it" message. You didn't think of that did you ?

    Quote Originally Posted by gkjr1 View Post
    I wasn't talking in riddles. I accidently deleted the "It's not showing anything" for errors part.
    No, you've been talking in riddles all through this thread as far as I can see. In your op, from my POV it looked clearly like a path issue and you seemed to be describing it as such. Clearly I wasn't the only one as AndrewGSW also thought so and replied on that basis.

    After this, you then go on to make the point that this is not a path issue at all and that we've got you wrong.

    You need to work on explaining things a bit more clearly so that other people can understand your point. Nothing in your opening thread could have ever indicated this was something to do with your database connection at all and we stood little chance of getting to that with the information you had supplied.

    Quote Originally Posted by gkjr1 View Post
    Dont' be a jerk it's unnecessary really.
    I am not. You've given conflicting information and ingnored my request to use error reporting in your code and when you did eventually do so, you did it in a blank php file. What was the point of that?

    Quote Originally Posted by gkjr1 View Post
    I posted never mind, and the solution, because the problem is solved.
    Yes but this was after I had read your previous (and only) reply and then hit reply myself, started typing away, scrolling down to re-read, scroll back up, edit bits, scroll back down re-read again, scratch head, scroll back up, re edit a few more times, give up and eventually hit "Subtmit Reply".

    You're trying to have a go at me because you didn't post your "Solved" message before I could actually see it? Seriously?

    Quote Originally Posted by gkjr1 View Post
    There really wasn't a need for the last post whatsoever, so yes, you are chasing your tail in circles but it is your own fault.
    What, because you posted your final reply AFTER I had seen the thread without it? - In other words it's my fault because I read the thread and hit reply BEFORE you posted your "Solved" message? You're talking rubbish I'm afraid.

    Quote Originally Posted by gkjr1 View Post
    If you don't understand and actually want clarification, asking more kindly is usually a good way to not upset people in a community, unless you're one who just doesn't care to do that
    You know something, you're the one who came here with the problem seeking help. You're the one who described it in such a way that we thought it was a file path issue. You're the one who couldn't get your point across properly, you're the one who failed to mention anything useful that could lead us to the database conclusion.

    I asked you to turn on error reporting, you're the one who ignored it. You then went about debugging it yourself properly and fixed it anyway without actually needing our help in the first place. In truth I suspect you only got to the bottom of it because you eventually followed through with my suggestion, turned on error reporting in the actual script and noticed a big error glaring you in the face.

    Of the only two people who tried to help you, I replied the most often in my efforts to help you. You're the one who knocked it.

    Quote Originally Posted by gkjr1 View Post
    so why are you trying to help anyone out in the first place.
    Well I generally try to help people solve their problems. Most of the time people are grateful for this however it is the internet, there will always be misunderstandings between people such as this where you posted a reply after I had read and started my reply.

    Quote Originally Posted by gkjr1;1309168[B
    ]Sorry I didn't mean to delete the first part of that sentence.[/B] It was an accident and doesn't deserve any rude replies ya know?
    But you're poking at me for my reply without even thinking that I might have never seen your final reply? So let me get this right, you can post a rude reply back to me being all snotty when I've posted a genuine reply however you are allowed to delete pieces of the puzzle, provide totally unclear and conflicting information and you expect to be all innocent?

    Quote Originally Posted by gkjr1 View Post
    Anyways, see how meaningless all that information was?
    Yes, all of it. You basically posted a thread which could not be solved based on the information you supplied. You then become aggressive when this is pointed out to you.

    Quote Originally Posted by gkjr1 View Post
    And talk about riddles (really? riddles) and chasing tails.....
    What I posted was clear information about the current working directory. If your script is in /public_html/ then that is its CWD. If you then include a script from /public_html/classes/ then the CWD is still /public_html/ inside the included script and NOT /public_html/classes.

    However, I note again from your text above, you're explaining how you're all innocent because you deleted text and supplied the wrong information etc and then you're taking a stab at ME for talking in riddles? - I gave you information based on the description of your problem - information that YOU supplied.

    Quote Originally Posted by gkjr1 View Post
    If you would have paid more attention to my op you should have noticed that the directories were in fact correct.
    You need to go away for a week and then re-read your op yourself - my points below are in red:

    I have a code to pull inactive users from the database, and show it up in a table. If i go to the php file (getunregusers.php) it shows up just fine You visit the file directly, it works fine., but using include, include_once, or require in either the head of the html or the body where i would want it to show isn't working at all and cuts off the rest of the script.Your include doesn't work. This could only happen due to a file path error.

    To add, I have a different php file to check login security for pages, and it's includes to connect to the database are the exact same as getunregusers.php and the security works fine, so I don't see any issues with the path to the files included/required. YOU don't see any issues with the file path. I do based on the information provided.
    Quote Originally Posted by gkjr1 View Post
    I'm not trying to be a jerk
    No comment.

    Quote Originally Posted by gkjr1 View Post
    but as advice maybe more kind replies would be benefitial to everyone?
    How was I not kind? I have provided information based upon your wrong information and I have asked you to use error reporting in the script which you then ignored and when I pushed you again, you decided not to do this but to use error reporting in a totally different file. Thats what I'd call taking the mickey and abusing someone who is trying to help.

    Quote Originally Posted by gkjr1 View Post
    I do appreciate the attempts for help.
    Clearly you don't because you've written an entire post bashing me because I tried to help you based on your wrongful information. You also bash me because I started my reply 7 minutes before your latest one. You then continue paragraph after paragraph bashing me because of what you have done.

    You now claim you appreciate my efforts? - after all that slagging off?

    You know what the worst thing is? When I said I felt like I was chasing my tail around in this topic I was letting off steam. It was not a direct insult to you. I did not call you a name, I did not say anything bad about you as a person, I just said how I FELT.

    You however are the total opposite and decided to slag me off publicly for as much as you could, run me down over everything that you had misrepresented and somehow you expected me to bend the laws of time and run me down because I didn't.

    Thanks
    Last edited by tangoforce; 01-28-2013 at 01:57 PM.
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    All right now, lets keep everything civil.
    Glad to hear that the issue was an alternate problem. From the first post, it really reads as being a pathing issue.
    My suggestion for this is to always work relative to eliminate this as a possibility:
    PHP Code:
    require_once __DIR__ '/../classes/aclass.class.php'// or even just /aclass.class.php if "this" script is also within classes 
    for example. The __DIR__ (or on older versions of PHP dirname(__FILE__)) will resolve the absolute filesystem path of this file, not the working directory. This is substantially more beneficial than the use of DOCUMENT_ROOT since document root only exists in the context of a web server. It won't work for executing cron jobs for example if they use the php executable directly, whilst the __DIR__ will.

    One thing to consider is to rename the files. If you use namespaces you can chain the class loading to the spl autoload by simply registering the extensions of files to load. This in turn is chained to both use and absolute namespace resolution:
    PHP Code:
    // root/proj/classes/myclass.class.php
    namespace proj/classes
    {
        use /
    proj/classes/anotherclass;
        class 
    myclass
        
    {
        }
    }

    // in some global script or a runner:
    set_include_path(get_include_path() . PATH_SEPARATOR '/root'); // /root is the 'root' directory here.  We want to seek namespaces "from" the /root
    spl_autoload_extensions('.class.php');
    spl_autoload_register();

    $obj = new /proj/classes/myclass(); 
    Note the namespaces are actually back slashes and not front slashes, but the PHP tags don't render them properly in the forums here.

    If you use a large amount of OOP, than I would suggest using namespaces, and also using suffix extensions instead of prefix ones (there is no way to register a prefix to the spl). You can write a new function to do so for you and add that to the register; however, even without there is overhead involved since it needs to logically check all the file names to find a match. Adding another function to prefix the name will add a bit of time (albeit you're looking at minimal addition to time).

    What I'm getting at here, is don't include files into your file with the assumption that an external file will dictate a new path. When you are already in the classes directory, if you issue a include('classes/class.mysqli.php'); it would work from a higher directory that is executing it, but won't work if you were to nest a new directory above this and call it from a file within (without altering cwd). It is much easier to simply use include(__DIR__ . '/class.mysqli.php'); instead.
    Also, with classes, try to avoid using include (include is a "best attempt" approach, when I'd bet that class.mysqli.php is *always* required) and opt for require, and always use *_once syntax especially on classes. If you end up in a situation where one file includes another class, and then a different include includes that same class, it will result in a fatal error when it attempts to load the class a second time (you cannot overwrite the symbol for the declared class). Using _once prevents that from occurring by checking if its been imported before trying to import it.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • Users who have thanked Fou-Lu for this post:

    gkjr1 (01-30-2013)

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by Fou-Lu View Post
    All right now, lets keep everything civil.
    I don't believe I was ever being intentionally rude. I never insulted the user but merely pointed out that they had not done as i requested and stated how I felt.

    Quote Originally Posted by Fou-Lu View Post
    From the first post, it really reads as being a pathing issue.
    Well I'm glad to see I'm not cracking up
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!


  •  

    Posting Permissions

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