title: php $_SERVER['PHP_SELF'] to include query string
body: <p>I'm essentially trying to make a login system that will return the user back to the page they were on. I know this question has been asked a bit, and I've looked at the other answers on SO, but I cannot find a solution to my particular problem.</p>
  
  <p>My site has has a table with reference id numbers (ex: 10001, 10003, 10004, ... 53401, etc.). These numbers are also links. All links point to one page ("mypage.php"), and the reference id number (10004) becomes a query string to that url:</p>
  
  <pre><code>&lt;td&gt;&lt;?php echo '&lt;a href="mypage.php?query_ecr=', urlencode($num), '"&gt;'; ?&gt;&lt;?php echo $num; ?&gt;&lt;/a&gt;&lt;/td&gt;
  </code></pre>
  
  <p>On my "header.php", which is on every page of the site, there is a button on the menu that will open the form below for the user to log in.</p>
  
  <pre><code>&lt;form action='login/process.php' method='post'&gt;
    &lt;label for='name'&gt;Username:&lt;/label&gt;
    &lt;input type='text' id='userid' name='user_name'/&gt;
    &lt;label for='password'&gt;Password:&lt;/label&gt;
    &lt;input type='password'  name='password' id='userpassword'/&gt;
  
    &lt;input type='submit' value='Log In' /&gt;
  
    &lt;input type='hidden' name='login' value='1'&gt;
    &lt;input type='hidden' value='".$_SERVER['PHP_SELF']."' name='redirurl'/&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Notice the "hidden input" with the name='redirurl'. I want to capture the current page the user is on. I already have a login script that will check the username and password and redirect them to the page they need to be.</p>
  
  <pre><code>//login/process.php
  
  ...blah blah blah other stuff...
  
  global $database,$session;
         $this-&gt;user_status=$database-&gt;CheckUserPass($_POST['user_name'],$_POST['password']);
         $url = $_POST['redirurl'];
         if($this-&gt;user_status==1)
            {
                $session-&gt;StartSession($_POST['user_name'],$_POST['password']);
                header("Location: ".$url);
          } else {
                ...blah blah blah.....
                }
  </code></pre>
  
  <p>My problem is that if the GUEST clicks on a link (ex: 10004), they are taken to the url:</p>
  
  <pre><code>http://www.XXXXXX.XXXX/mypage.php?query_ecr='10004'
  </code></pre>
  
  <p>However, on that page the value for 'redirurl' is:</p>
  
  <pre><code>http://www.XXXXXX.XXXX/mypage.php
  </code></pre>
  
  <p>It disregards the query string. So when the user logs in from that page, and my login script re-directs them back to that page, the page has a ton of errors because it needs the query string.</p>
  
  <p>How do I include the query string in: $_SERVER['PHP_SELF'] ?</p>