-1

i want to create a link like the below:

<a href="'.$_SERVER["REQUEST_URI"].'?&action=approve&holiday='.$result["sequence"].'">

the $_SERVER["REQUEST_URI"] includes any $_GET variables already set, but i am not sure whether to put a ? or & after this in the href because $_SERVER["REQUEST_URI"] could already include a $_GET variable therefore it would need & and not a ?

charlie
  • 415
  • 4
  • 35
  • 83
  • i cant see where this helps with adding either `?` or `&` – charlie Jul 28 '16 at 18:43
  • I think you would have to check $_SERVER["REQUEST_URI"] content, in order to see if it already contains any GET variables. For example: if ($_SERVER["REQUEST_URI"] contains '?') then you use "&" to include your content, if not then you can use "?". Something like that? – Guilherme Vaz Jul 28 '16 at 18:46
  • 1
    See this previous post http://stackoverflow.com/questions/7864237/php-to-check-if-a-url-contains-a-query-string – jasonlam604 Jul 28 '16 at 18:47
  • Use my updated answer – Mojtaba Jul 28 '16 at 18:57

2 Answers2

1

Check if it includes '?' or not.

$extra = 'action=approve&holiday='.$result["sequence"];
$glue = (strpos($_SERVER["REQUEST_URI"], '?') === false) '?' : '&';

Then, you can use this:

 echo '<a href="'.$_SERVER["REQUEST_URI"]. $glue . extra .'">';

But, if you don't need the current passed parameters in URL, you can use the way @Utkanos said

Mojtaba
  • 4,852
  • 5
  • 21
  • 38
0

You need to build the URL in parts. All of the data you need is contained in the $_SERVER superglobal.

$_SERVER['REQUEST_SCHEMA'].'://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'//...

PHP_SELF denotes the URI beyond the hostname, e.g. "/foo/bar.htm" in "mydomain.com/foo/bar.htm"

http://php.net/manual/en/reserved.variables.server.php

Mitya
  • 33,629
  • 9
  • 60
  • 107