Page 1 of 1

own URL[solved]

Posted: Sun May 23, 2004 9:21 am
by vigge89
I want to get the filename and querystring of the script running (to generate links and things like that), so i tried using this code:

Code: Select all

<?php
### set path info
$fself		= $_SERVER['PHP_SELF'];
$qstring	= $_SERVER['QUERY_STRING'];
if (empty ($qstring)) $fself = $self."?";
if (!empty ($qstring)) $fself = $self."?".$qstring."&";
?>
i then generate links with this for example:

Code: Select all

<?php
echo "<a href='{$fself}n={$rndnumber}'>link</a>";
?>
however, the querystring doesn't get deleted, so if i click the link 2 times or more, or if i go to another page, the querystring is still there. after some clicks, it could look like this:

Code: Select all

index.php?p=news&amp;n=2&amp;n=3&amp;n=5&amp;n=4&amp;n=1&amp;n=3&amp;n=2
so i'm wondering if there's someway else to do this?
i can't come up with anything...

Posted: Sun May 23, 2004 9:41 am
by kettle_drum
Just loop through $_GET adding the key and value seperated by "=" and "&".

Posted: Sun May 23, 2004 9:46 am
by vigge89
you mean something like this?

Code: Select all

<?php

### set path info
$self      = $_SERVER['PHP_SELF'];

$qstring = "";
foreach ($_GET as $key => $value) {
    $qstring .= "$key=$value";
}

$fself = $self."?".$qstring;

?>

Posted: Sun May 23, 2004 9:47 am
by kettle_drum
Yeah, and if you still get duplicate values then just run $_GET though array_unique();

Posted: Sun May 23, 2004 9:48 am
by vigge89
ok, i'll try it out after i'm finished with some C++ studying :wink:

Posted: Sun May 23, 2004 11:29 am
by vigge89
aww, i have this:

Code: Select all

<?php
### set path info
$_GET = array_unique ($_GET);
$self = $_SERVER['PHP_SELF'];

$qstring = "";
foreach ($_GET as $key => $value) {
	$qstring .= "$key=$value&";
}

$fself = $self."?".$qstring;
?>
but there's still one duplicate :?

is there any better way of doing this?

Posted: Sun May 23, 2004 2:43 pm
by vigge89
got it too work with this:

Code: Select all

<?php
$self = $_SERVER['PHP_SELF'];

$qstring = "p=".$_GET['p'];
if ($qstring != "p=") $qstring .= "&";

$fself = $self."?".$qstring;
?>