URL Question

zenlok

New Member
Messages
14
Reaction score
0
Points
0
Hi All,

I am currently designing a site and was wondering if anyone knew of a script that allowed the URL showed in the address box to be showed as something like;

http://mysite.com/index.php?=home

And changing "home" to something else to link to another page.

Any help or ideas would be greatly appreciated.

Many thanks,

ZeNLoK
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
You could do that with some basic php. It's easier if you'd want http://mysite.com/index.php?page=home or something like that (notice the 'page=' instead of '=').

If it was the link I posted you could have a code like this:

PHP:
<?php
if($_GET["page"] == "home") {
include "home.php";
exit;
}
?>
<html>
<!-- real index.php -->
</html>
This script would import from "home.php" when page=home is specified.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
if you don't want the "page=" part to show up: http://mysite.com/index.php?home

There is a slightly different procedure.

PHP:
<?php
if (isset($_GET['home'])) {
    //load page named "home"
} elseif (isset($_GET['something'])) {
   //load page named "something"
} else {
   //here load a default page, for instance an error message or something like that to tell users that the page they choose does not exist.
}
?>
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
For a fully general solution,

1. Put your files in a subdirectory like site_pages, so it will be harder for users to go directly to the page.

2. I assume they will all have .php extensions

3a. URL of "index.php?show=portfolio" (substitute 'page', 'request' , 'q' etc for 'show' )
PHP:
$directory = 'site_pages/' ;
$default_page = 'site_pages/default.php' ;

if( isset( $_GET[ 'show'] ){

  $stub = $_GET[ 'show'];
 
  if( preg_match(  '/(\.|\/|\\)/' , $stub ) ){   ## make sure no .  or /  or \  to foil hackers 
      $requested_page = $default_page ;
  } else {
      $requested_page = $directory . $stub . ".php" ;
      if( ! is_file( $requested_page ) ){  ## no file, serve up default
         $requested_page =  $default_page ;      
     } 

} else {

  $requested_page =  $default_page ;

}

include( $requested_page ) ;

exit() ;
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
3b URL of "index.php?portfolio" (substitute the page name for 'porfolio' ). Add a elseif (...) { ... } block for each page you make.
PHP:
<?php
$directory = 'site_pages/' ;
$default_page = $directory .  'default.php' ;

if ( isset( $_GET[ 'index'] )){
  $stub = 'index';
} elseif ( isset( $_GET[ 'page2'] )){
  $stub = 'page2';
} elseif ( isset( $_GET[ 'page3'] )){
  $stub = 'page3';
} else {
  $stub =  'default';
}

$requested_page = $directory . $stub . ".php" ;
if( ! is_file( $requested_page ) ){
   // no file, serve up default
   $requested_page =  $default_page ;      
} 

include( $requested_page ) ;

exit();
?>

descalzo, you could improve your code by doing a switch... it would be more efficient.
Oh and you forgot a ) after the if statement.
 
Last edited:

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
descalzo, you could improve your code by doing a switch... it would be more efficient.

But then if he adds 15 new pages, he has to add 15 new cases to the switch, with the possibility of misspelling a case, etc.
 

xav0989

Community Public Relation
Community Support
Messages
4,467
Reaction score
95
Points
0
But then if he adds 15 new pages, he has to add 15 new cases to the switch, with the possibility of misspelling a case, etc.
true... I need to stop thinking in terms of my site (my pages do not change much)

have you seen your typo?
 
Last edited:

marshian

New Member
Messages
526
Reaction score
9
Points
0
I'd stay away of any "general" possibilities, if you mess up the slightest bit a hacker can find an easy way in. Maintaining a list of pages is far better.

Why do you want to be able to do this? If you are just curious as to how it's possible I think the answer has been given many times.
There are a number of downsides to this too. You'd have to update that page if you want to add a new page. (Did I hear the word typo?) I think it'd be more efficient to just add a file. Also, some search engines don't like urls like that and will just skip them, assuming it's dynamical generated content, not a static page (usually they're only interested in static pages).
Most of the time you hear questions like this:
I have this news page with urls like this: news.php?id=15 but I've seen many sites that use urls like news/15/ instead. How is it possible to do this?

So it's up to you, but in general there would be no need to do this.
 
Top