PHP link in the adress bar

Joined
Oct 18, 2005
Messages
181
Reaction score
0
Hello, I want to know how to make in the adress bar link on php

Example:
I have index.php and about.php
How to make
adres/index.php?about

and it will show about.php?

I have tried
<php
if(!empty($_GET['about'])){ include('about.php'); die();}
>

But it doesn't work as good as i want, it shows
adres/about
for about.php , but i need index.php?about or index.php?op=about

Please, if you know how to do it, help me. :lamo:
 
Last edited:
try this
PHP:
if(isset($_GET['op'])) {
   if(file_exists($_GET['op'] . '.php')) {
      include($_GET['op'] . '.php');
   } else {
      if(!empty($_GET['op'])) {
         echo "Please enter a valid page.";
      } else {
         echo "Please enter a page to go to.";
      }
   }
}

or if you want to make your code run faster, use cases.

[N]asser` ~ Out
 
yes, It suppose to go to the index.php

[N]asser, thanx for code, but there is error, I do not know where, but if I'm pressing link, it goes into index.php, always.
 
Last edited:
Mr.X said:
Code:
<?php

$page = $_GET['page'];
$extension = '.php';

$page = $page . $extension;

if(!file_exists($page)) {

include 'index.php';

}
else {

[b]include $page . '.php';[/b]

}

?>

Try that.
look at the bolds. doesn't $page already include $extension (.php)?
 
Besides, if errors are on and they havn't set $_GET['page'], it'll display an error.

PS - Gashek, try to go to index.php?op=about. It won't redirect you to about.php, but just includes it. If you want to redirect to that page, use the following code:
PHP:
if(isset($_GET['op'])) {
   if(file_exists($_GET['op'] . '.php')) {
      header("Location: " . $_GET['op'] . '.php');
   } else {
      if(!empty($_GET['op'])) {
         echo "Please enter a valid page.";
      } else {
         echo "Please enter a page to go to.";
      }
   }
}

[N]asser` ~ Out
 
That code is insecure. Don't use it.

Here's a bit of sourcecode from Volamir:
PHP:
$params = array_slice(explode("/",checkVar(strtolower($_SERVER["REDIRECT_URL"]))),1);
$userdata = getUserData();

switch ($params['0']) {
  case 'accnt':
    if($userdata or $params['1'] == ('new' || 'save')) {
    	include("account.inc.php");
			accountHandler($params['1']);
			break;
		}
	case 'chars':
		if($userdata) {
			include("charSelect.inc.php");
			charHandler($params['1'], $params['2']);
			break;
		}
	 case 'battle':
	 	if($userdata) {
	    include("battlefield.inc.php");
  	 	battlefield($params['1']);
	 		break;
	 	}
  case 'index':
	 	default:
		welcome();
	 	break;
}

Subsequentally, the different cases load their own template:
PHP:
function newAccount() {
	/* LOAD NEW-ACCOUNT TEMPLATE */
	$smarty = new smarty_connect;
  	$smarty->caching = true;
  	$smarty->display('newAccount.tpl');
}

Works fast as hell due to Smarty (which can cache template files and output) and is incredibly easy to maintain due to each page has its own case.

Now, if I for example go to fragfrog.nl/accnt/new/ it'll automatically display the new account form.

I suggest you start reading http://smarty.php.net :)

(by the way, this won't work directly if you copy paste it since you don't have my include file (which for example has the checkVar function that prevents any injection whatsoever). Source protection and all that. But it should give you a pretty good idea what I'm talking about.)
 
Last edited:
Back