/    Sign up×
Community /Pin to ProfileBookmark

www.domain.com/index.php?home how do this?

On various websites I see adresses such as this one:
[url]www.domain.com/index.php?home[/url]
and I think I know how to do it…

But I’m not sure.

The way I assumed it was done was by pulling say, a name attribute from a link tag <a href=”index.php” name=”home”> enclosed in a <form action=”index.php” method=”GET”> and then index.php pick it up with a $GET_name attribute in index.php and places it inside <?php include ‘$link’;?>.

Is this how its done? And if not how would you do it? is there a Tutorial?

Cheers
hamstar

to post a comment
PHP

16 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMay 25.2005 — Simpler is just make the link tag:
[code=html]<a href="index.php?home">Home Page</a>[/code]
For index.php:
[code=php]
$keys = array_keys($_GET);
$inc = "/path/to/includes/{$keys[0]}.inc";
if(file_exists($inc))
{
include $inc;
}
else
{
# output an error message and/or default page here
}
[/code]
Copy linkTweet thisAlerts:
@ShmohelMay 25.2005 — By having a link like: <a href='index.php?page=home>Link</a>

you can then put something like:
[code=php]
if ($_GET['page']=='home') {
//DO SOMETHING HERE
}
[/code]


A lot of times I will use this sort of thing to include other pages etc.
Copy linkTweet thisAlerts:
@hamstarauthorMay 26.2005 —  $keys = array_keys($_GET);

$inc = "/path/to/includes/{$keys[0]}.inc"; [/quote]


I don't understand this part...
Copy linkTweet thisAlerts:
@NogDogMay 26.2005 — I don't understand this part...[/QUOTE]
The data following the ? in a URL is saved to the $_GET array by PHP, with each "name=value" pair saved to the array with 'name' as the array key and 'value' as the value of that array entry. So I'm simply (?) looking at the first key (numbered 0) in the $_GET array, which in this case has no value assigned, but I'm not looking at the value so don't care.
Copy linkTweet thisAlerts:
@floydiologyMay 27.2005 — very simple : <a href="page.php?variableA=" . <? echo $value; ?> . "&variableB=" . <? echo $valueB; ?> .">link</a>
Copy linkTweet thisAlerts:
@hamstarauthorMay 27.2005 — Sweet, cheers guys got it now...

I settled with a hybrid of the two ?
[code=php]
<a href="index.php?home"></a>

<?php

$intget = $_GET;

if($intget != "home"|"badpractices"|"petition"|"links"|"about"|"contacts")
{
$page = "home";
}
else
{
$page = $intget;
}

?>

<?php include '$page';?>[/code]


That'll work huh?
Copy linkTweet thisAlerts:
@ShmohelMay 27.2005 — almost, but not quite. The [color=red][i]include '$page'[/i][/color] first does not need the quotes around $page. Second, you would need to put this inside the IF and ELSE brackets. You define the variable [i]page[/i] but it only exists within the brackets.

Second, I have never tried the OR clause as you are using it, I do not think it will work like that. try something like this:

[code=php]

<a href="index.php?page=home"></a>

<?php

$intget = $_GET['page'];

if($intget != "home" || $intget !="badpractices" || $intget !="petition" || $intget !="links" || $intget !="about" || $intget !="contacts")
{
$page = "home";
include $page.".php";
}
else
{
$page = $intget;
include $page.".php";
}

?>
[/code]
Copy linkTweet thisAlerts:
@chrysMay 27.2005 — That won't work. You need to use $_GET[0] ( the first element in the $_GET array )... otherwise you will be checking to see if the Array = "page", which will never work.
Copy linkTweet thisAlerts:
@ShmohelMay 27.2005 — That won't work. You need to use $_GET[0] ( the first element in the $_GET array )... otherwise you will be checking to see if the Array = "page", which will never work.[/QUOTE]

amended.
Copy linkTweet thisAlerts:
@hamstarauthorMay 28.2005 — What about if I define $page outside the brackets, like with js?

Also, how can I put more things into the GET array? commas? semicolon?

eg: /index.php?page;time;banana
Copy linkTweet thisAlerts:
@ShmohelMay 28.2005 — What about if I define $page outside the brackets, like with js?

Also, how can I put more things into the GET array? commas? semicolon?

eg: /index.php?page;time;banana[/QUOTE]


You are trying to get [color=red]$page[/color] to have different values at different conditions, so defining it outside the brackets defeats the purpose of the brackets.

for your other question, just look at the address bar for this page:
http://www.webdeveloper.com/forum/newreply.php?do=newreply&amp;p=376735

If there were a [color=red][i]print_r[/i]($_GET)[/color] you would see the GET array displayed as such:

<i>
</i>array (
[do] =&gt; newreply
[p] =&gt; 376735
)


Therefore, these items can be recalled as [color=red]$_GET['do'][/color] & [color=red]$_GET['p'][/color]

All variables passed through the url should be in the form [color=red]page.php?a=123&b=456&c=789[/color]
Copy linkTweet thisAlerts:
@JeffroMay 30.2005 — If you need more info, go here: http://xentrik.net/php/querystring.php
Copy linkTweet thisAlerts:
@hamstarauthorMay 31.2005 — Well I got it to work.

In the end I used a switch case function...[code=php]
<?php
$intget = $_GET;
switch($intget) {
case "home": include $intget; break;
case "badpractices": include '/badpractices'; break;
case "petition": include '/petition'; break;
case "feedback": include '/feedback'; break;
case "links": include '/links'; break;
case "about": include '/about'; break;
case "contact": include '/contact'; break;
case "sitemap": include '/sitemap'; break;
default: include 'http://www.telescum.co.nz/home';
}

?>[/code]

However, it won't do relative inclde links...

I have to give it the whole one. It gives the following error:

Warning: main(/newsdocs/news.php): failed to open stream: No such file or directory in /var/www/html/index.php on line 58

Also when I try to grab GET[0] it says:

Notice: Undefined offset: 0 in /var/www/html/index.php on line 39

So I set it to just GET but now it always goes strate for the default case...?

confused...
Copy linkTweet thisAlerts:
@hamstarauthorMay 31.2005 — so really, I didnt get it to work :S
Copy linkTweet thisAlerts:
@hamstarauthorJun 02.2005 — help?
Copy linkTweet thisAlerts:
@ScleppelJun 02.2005 — [CODE]
$intget = trim($_SERVER['QUERY_STRING']);
[/CODE]
×

Success!

Help @hamstar spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...