Question

when someone arrives at my site i would like the URL to be at to index.php?p=home

 <?php
  if (file_exists("pages/$p.php")) {
    include("pages/$p.php");
}else{
 header("Location: index.php?p=home");
}
?>

I need this to show up in the URL as if they were landing on that page. as of write now it just lands on my index page till you click a link to open.

I tried making this work with the header location but it does not work... Also I used Include and that works but it does not show the path in the url. I need the path to be correct as if they clicked the home link.

Was it helpful?

Solution

I don't know what you are planning to do but this seems to be a bad approach.

Add this to your index.php:

Old answer    
    /*
    if(!isset($_GET['p'])){
        header("Location: index.php?p=home");
    }
    */

Edited:

Since it seemed like your header() function did not work because of probably any output before the function call, changed answer to:

$page = basename($_SERVER['PHP_SELF']); 
if($page=='index.php' && !isset($_GET['p'])){ 
echo '<meta HTTP-EQUIV="REFRESH" content="0; url=index.php?p=home">'; 
} 
$p = (isset($_GET['p'])?$_GET['p']:'home'); 
if (file_exists("pages/{$p}.php")) { 
include("pages/{$p}.php"); 
}

OTHER TIPS

<?php
  $p = $_GET['p'];
  if (file_exists("pages/{$p}.php")) {
    include("pages/{$p}.php");
}else{
 if ($p != "home")
  header("Location: index.php?p=home");
}
?>

Note that this code is not secure as any user may use to to access hidden files and directories.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top