Jump to content

Issue with duplicate '?' in query string


Stefan83

Recommended Posts

HI

I'm building a splash page that mobile users are redirected to when they visit a page on my website. from=(Original URI) is added the splash page's URL. There is a link at the bottom of the splash page which gets the value of the 'from' query to allow the user to click back to the orginal page.

I'm trying to build a URL for this link with the following script so '?full=1' query string is appended to the end of the URL. Here's the code:

<?php
$qstring = array('full'=>'1'
              );
?>
<a href="<?php bloginfo('url'); echo htmlspecialchars($_GET["from"]); ?>?<?php echo http_build_query($qstring) . "\n"; ?>">Continue to Website</a>

- get the current URL of a Wordpress site (eg http:www.domain.com)
- echo the value of 'from' query (eg from=orginalpage)
- and append the full=1


The problem is if there is a query string already in the from URI from original page , I get an unwanted duplicate '?' in the URL. So if the original URL is http://www.domain.com/orginalpage/?test=1

would redirect to

http://www.domain.com/splashpage/?from=orginalpage/?test=1

So the link looks like

http://www.domain.com/orginalpage/?test=1?full=1

How do I stop the duplicate '?' and replace with '&'?

Thanks
 

Link to comment
https://forums.phpfreaks.com/topic/285237-issue-with-duplicate-in-query-string/
Share on other sites

You could try

<?php
// get the query string and parse into an array
parse_str($_SERVER['QUERY_STRING'], $queryStringArray);

// add full to array
$queryStringArray['full'] = 1;

// rebuild the query string
$queryString = '?' . http_build_query($queryStringArray);
?>
<a href="<?php bloginfo('url'); echo $queryString; ?>">Continue to Website</a>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.