PHP require_once $_GET error

Use this exact same template on other pages of my website, and I’m trying to include some CSS in my beginning.php if a $_GET[‘demos’] is set.

[14-Jan-2015 15:42:49 America/Denver] PHP Warning:  require_once(/home1/codefund/public_html/../includes-test/beginning.php?demos=valr): failed to open stream: No such file or directory in 
 /home1/codefund/public_html/demos-test/vertical-align-left-right.php on line 3
    [14-Jan-2015 15:42:49 America/Denver] PHP Fatal error:  require_once(): Failed opening required '/home1/codefund/public_html/../includes-test/beginning.php?demos=valr' (include_path='.:/usr/php/54/usr/lib64:/usr/php/54/usr/share/pear') in /home1/codefund/public_html/demos-test/vertical-align-left-right.php on line 3

This is my beginning.php

<?php 
error_reporting(E_ALL); ini_set('display_errors', 'On');
session_start();
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title><?php echo $page;?> - CodeFundamentals</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta charset="UTF-8">
<meta name="Description" content="CodeFundamentals offers a wide range of useful articles aimed at web designers. The articles cover a wide berth of topics in CSS and HTML. We also are available for web design services and offer competitive prices. Come check us out today and get your free quote!">
<meta name="Keywords" content="CSS, HTML, web, SEO, Accessibility, blog, portfolio, web designer, hire, articles, links, demos">
<link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
<link rel="icon" href="/favicon.ico" type="image/x-icon">
<link href="http://fonts.googleapis.com/css?family=PT+Sans" rel="stylesheet" type="text/css">
<link href="http://fonts.googleapis.com/css?family=Roboto|Roboto+Condensed" rel="stylesheet" type="text/css">
<link href="/scripts/resets.css" type="text/css" rel="stylesheet">
<link href="/scripts/styles.css" type="text/css" rel="stylesheet">
<link href="/scripts/code-script.css" type="text/css" rel="stylesheet">
<style type="text/css">
  @import url(http://www.google.com/cse/api/branding.css);
</style>
<?php
if(isset($_GET['demos']))
{
  if($_GET['demos']==="valr")
  {
    echo '<style type="text/css">
.block
{
  overflow:hidden;
  text-align:justify;
  /* IE special */  
  width: 100%;  
  -ms-text-justify: distribute-all-lines;
  text-justify: distribute-all-lines;
}
.block:after
{
  content: " ";
  display: inline-block;
  width: 100%;
  height: 0;
  font-size:0;
  line-height:0;
}
.div_location_description
{
  display:inline-block;
}
.div_address
{
  display:inline-block;
  vertical-align: baseline; 
}
</style>';
  }
}
?>
</head>
<body>

This is my page that fails. Only difference that makes this fial is line 3. Dunno why though.

<?php
$page="Template";
require_once($_SERVER['DOCUMENT_ROOT'].'/../includes-test/beginning.php?demos=valr');echo "\n";
require_once($_SERVER['DOCUMENT_ROOT'].'/../includes-test/header.php');echo "\n";
require_once($_SERVER['DOCUMENT_ROOT'].'/../includes-test/nav.php');echo "\n";?>
  <div class="main-wrap">
    <div class="main">
      <div class="content">
        <div class="container">
          <div class="block">
            <div class="div_location_description">Room ABC</div>
            <div class="div_address">
              <div>Sample Address</div>
              <div>123 Main St</div>
              <div>Anywhere, NY 12345</div>
            </div>
          </div>
        </div>
      </div>
<?php require_once($_SERVER['DOCUMENT_ROOT'].'/../includes-test/sidebar.php');echo "\n";?>
    </div>
  </div>
<?php require_once($_SERVER['DOCUMENT_ROOT'].'/../includes-test/footer.php');echo "\n";?>

So that i’m clear, beginning.php is in a directory one level above the root of the webpages, in a directory called includes-test?

Yes. It’s saying it’s a 404 but its not a 404. If I remove the ?demos=valr from the requrie string, it will find it. The file is 100% there and only the code from the $_GET and ?demos=valr is causing the issue. I’m 100% certain of this.

I got it working by not passing the string in the $_GET. I manually set a variable $demosVersion and I do the isset() there and check the value. Very weird that the require failed though. Thoughts would be appreciated.

Yup, require_once is expecting a file path, not a URL, so you can’t include query string params…

That’s a shame. Not a huge deal though - I wasn’t dead-set on doing the query string parameter way so I’ll stick with my way. Thanks guys.

I don’t know as I’d keep it as a $_GET but maybe

<?php
$page="Template";
$_GET['demos'] = "valr";
require_once($_SERVER['DOCUMENT_ROOT'].'/../includes-test/beginning.php');echo "\n";

Almost exactly as what I did but instead of $_GET, I did a regular variable.

I think for the most part that would be the better thing to do.
Since $_GETs are global it seems risky to be using them like that unless you give very careful thought and have a good reason to

I was using $_GET in a require so I found there no chance for users to modify it. Plus it woulda been easier to just throw the query parameter in instead of manually declaring a variable. I was unaware of the require_once restrictions though so I guess after all is said and done, the coding Gods are looking out for me. :slight_smile: .

In hindsight I guess this way is the way I should have done from the get-go.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.