Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1098991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:39:54+00:00 2026-05-17T00:39:54+00:00

The variable ‘ $return10 ‘ (for example) is a url, and I need to

  • 0

The variable ‘ $return10 ‘ (for example) is a url, and I need to append ‘ &var2=example ‘ to the end. Like this:

 header( "Location: $return10&var2=example" );
 header ("Content-Length: 0");
  exit;

The challenge is not knowing if the url contained in ‘ $return10 ‘ will already have a query string.

Choice A) If I use ‘ &var2=example ‘ , then sometimes the final url will be ‘ ://example.com&var2=example ‘ , with no ‘?’ to start the query string.

Choice B) If I use ‘ ?var2=example ‘ , then sometimes the final url will contain two “?”‘s starting two different query strings??

Is there a third choice? How would you cover both possibilities using “the correct code?” Thank you.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T00:39:54+00:00Added an answer on May 17, 2026 at 12:39 am

    Create a function that will append your query code if there is one… And add it if there isn’t…

    function append_query($url, $query) {
      // Fix for relative scheme URL
      $relativeScheme = false;
      if(substr($url, 0, 3) == '://') {
        $relativeScheme = true;
        $url = 'a' . $url;
      }
    
      $newUrl = http_build_url($url, array('query' => $query), HTTP_URL_JOIN_QUERY);
    
      if($relativeScheme) {
        return substr($newUrl, 1);
      }
    
      return $newUrl;
    }
    
    header('Location: ' . append_query($return10, 'var2=example'));
    

    This will work regardless of if your query has a fragment or not.

    EDIT: Fixed for relative scheme URL.

    If your PHP does not have http_build_url() available (ie.: PECL extension not installed), here is a pure PHP version of it which does not require the extension.

    define('HTTP_URL_REPLACE', 1);          // Replace every part of the first URL when there's one of the second URL
    define('HTTP_URL_JOIN_PATH', 2);        // Join relative paths
    define('HTTP_URL_JOIN_QUERY', 4);       // Join query strings
    define('HTTP_URL_STRIP_USER', 8);       // Strip any user authentication information
    define('HTTP_URL_STRIP_PASS', 16);      // Strip any password authentication information
    define('HTTP_URL_STRIP_AUTH', 32);      // Strip any authentication information
    define('HTTP_URL_STRIP_PORT', 64);      // Strip explicit port numbers
    define('HTTP_URL_STRIP_PATH', 128);     // Strip complete path
    define('HTTP_URL_STRIP_QUERY', 256);    // Strip query string
    define('HTTP_URL_STRIP_FRAGMENT', 512); // Strip any fragments (#identifier)
    define('HTTP_URL_STRIP_ALL', 1024);     // Strip anything but scheme and host
    
    // Build an URL
    // The parts of the second URL will be merged into the first according to the flags argument.
    //
    // @param mixed     (Part(s) of) an URL in form of a string or associative array like parse_url() returns
    // @param mixed     Same as the first argument
    // @param int       A bitmask of binary or'ed HTTP_URL constants (Optional)HTTP_URL_REPLACE is the default
    // @param array     If set, it will be filled with the parts of the composed url like parse_url() would return
    function http_build_url($url, $parts = array (), $flags = HTTP_URL_REPLACE, &$new_url = false) {
      $keys = array (
        'user',
        'pass',
        'port',
        'path',
        'query',
        'fragment'
      );
    
      // HTTP_URL_STRIP_ALL becomes all the HTTP_URL_STRIP_Xs
      if ($flags & HTTP_URL_STRIP_ALL) {
        $flags |= HTTP_URL_STRIP_USER;
        $flags |= HTTP_URL_STRIP_PASS;
        $flags |= HTTP_URL_STRIP_PORT;
        $flags |= HTTP_URL_STRIP_PATH;
        $flags |= HTTP_URL_STRIP_QUERY;
        $flags |= HTTP_URL_STRIP_FRAGMENT;
      }
      // HTTP_URL_STRIP_AUTH becomes HTTP_URL_STRIP_USER and HTTP_URL_STRIP_PASS
      else if ($flags & HTTP_URL_STRIP_AUTH) {
        $flags |= HTTP_URL_STRIP_USER;
        $flags |= HTTP_URL_STRIP_PASS;
      }
    
      // Parse the original URL
      $parse_url = parse_url($url);
    
      // Scheme and Host are always replaced
      if (isset($parts['scheme']))
        $parse_url['scheme'] = $parts['scheme'];
    
      if (isset($parts['host']))
        $parse_url['host'] = $parts['host'];
    
      // (If applicable) Replace the original URL with it's new parts
      if ($flags & HTTP_URL_REPLACE) {
        foreach ($keys as $key) {
          if (isset($parts[$key]))
            $parse_url[$key] = $parts[$key];
        }
      } else {
        // Join the original URL path with the new path
        if (isset($parts['path']) && ($flags & HTTP_URL_JOIN_PATH)) {
          if (isset($parse_url['path']))
            $parse_url['path'] = rtrim(str_replace(basename($parse_url['path']), '', $parse_url['path']), '/') . '/' . ltrim($parts['path'], '/');
          else
            $parse_url['path'] = $parts['path'];
        }
    
        // Join the original query string with the new query string
        if (isset($parts['query']) && ($flags & HTTP_URL_JOIN_QUERY)) {
          if (isset($parse_url['query']))
            $parse_url['query'] .= '&' . $parts['query'];
          else
            $parse_url['query'] = $parts['query'];
        }
      }
    
      // Strips all the applicable sections of the URL
      // Note: Scheme and Host are never stripped
      foreach ($keys as $key) {
        if ($flags & (int)constant('HTTP_URL_STRIP_' . strtoupper($key)))
          unset($parse_url[$key]);
      }
    
      $new_url = $parse_url;
    
      return ((isset($parse_url['scheme'])) ? $parse_url['scheme'] . '://' : '') . ((isset($parse_url['user'])) ? $parse_url['user'] . ((isset($parse_url['pass'])) ? ':' . $parse_url['pass'] : '') . '@' : '')
        . ((isset($parse_url['host'])) ? $parse_url['host'] : '') . ((isset($parse_url['port'])) ? ':' . $parse_url['port'] : '') . ((isset($parse_url['path'])) ? $parse_url['path'] : '')
        . ((isset($parse_url['query'])) ? '?' . $parse_url['query'] : '') . ((isset($parse_url['fragment'])) ? '#' . $parse_url['fragment'] : '');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider variable like var url='http://www.example.com/index.php?id=ss'; or var url='http://www.example.com/dir1/dir2/index.php'; In these variables, i want to
The variable x is getting the current url. I need to then put this
I have the following variable: pageID = 7 I'd like to increment this number
A variable returns MINGW32_NT-5.1 or CYGWIN_NT-5.1. (yea, dot at the end) Need to compare
JSTL variable values are not shown in EL. For example this code: <%@ taglib
Before variable-length arrays were supported, I would dynamically allocate them like this: int foo(size_t
The variable @photo contains this XML: <photo id=2733> <urls> <url type=photopage>http://www.flickr.com/photos/bees/2733/</url> </urls> </photo> I
String variable stores value as like .. careteam order 4-26-11.csv i need to trim
Variable names in haskell need to be in small case, but How to declare
I have an XML variable with only one element in it. I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.