0

Hi everyone I'm trying to make a dynamic web page with PHP and I have a problem with $_SERVER['PATH_INFO'], I think. Every time I press home or about it appends index.php.home or index.php.about to the url. Here is my code:

<body>
<div style="width:800px; height:auto;">
<nav>
    <a href="index.php/home">home</a>
    <a href="index.php/about">about</a>
</nav>
<?php 
    $path = substr($_SERVER['PATH_INFO'],1);
    echo $path;
    if($path==""){
        $path = "home";
    }
    if($path == "home"){    
        ?>
        <h1> Home Page </h1>
        <?php 
    }
    elseif($path == "about"){
        ?>
        <h1> About Page</h1>
        <?php 
    }else{
        ?>
        <h1> Page Not Found </h1>
        <?php 
    }
?>
</div>
</body>

Could I have some help with this problem?

2
  • If you run into a problem with some language feature, visit the manual: $_SERVER - you will find more information and even user-notes that help you to go around the very first corners. What does 'ORIG_PATH_INFO' give you? Commented Aug 6, 2012 at 8:16
  • Try using absolute urls like for example <a href="/index.php/about">about</a> or <a href="/path/to/index.php/about">about</a>. This will overcome your issue. Commented Aug 6, 2012 at 8:18

2 Answers 2

2

You should use $_GET variable for this. If you have an url like

index.php?p=categories&sp=specific_category

then you'll have:

$_GET['p'] will be categories
$_GET['sp'] will be specific_category

so

<body> 
<div style="width:800px; height:auto;"> 
<nav> 
<a href="index.php?p=home">home</a> 
<a href="index.php?p=about">about</a> 
</nav> 
<?php  
    if(!isset($_GET['p']) || $_GET['p'] == 'home'){ 
?>
<h1> Home Page </h1>
<?php
    } 
    elseif($_GET['p'] == "about"){ 
?> 
<h1> About Page</h1> 
<?php  
    } else { 
?> 
<h1> Page Not Found </h1> 
<?php  
    } 
?> 
</div> 
</body> 
Sign up to request clarification or add additional context in comments.

Comments

0

Put a complete path like this

<?php
$protocall="http://www.";
$host= $_SERVER['SERVER_NAME'];
$ext=".com";
$site=$protocall.$host.$ext;

?>

<nav>
    <a href="<?php echo $site; ?>/index.php/home">home</a>
    <a href="<?php echo $site; ?>/index.php/about">about</a>
</nav>

define the $site in a global file

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.