Jump to content

Accept %26 in query?


d3ll

Recommended Posts

Hi All,

 

Just joined up, ive only been dabling with PHP for the last few days but so far so good.. :)

 

Anyway, is there a way to accept %26 as a query separator instead of an &? Ive read conflicting info and havent been able to get anywhere.

 

I have a PHP script that makes a simple SQL query, all was going well but in some cases it is being passed an encoded URL, which breaks the query, eg:

 

?var1=a&var2=b in the URL works fine but ?var1=a%26var2=b does not..

 

Anyone any ideas?

Link to comment
Share on other sites

Well, wherever you're using the string that you need to replace the %26 with an &

 

Try putting this at the top of your script:

<?php
function replace_26_percent($url)
{
return str_replace('%26', '&', $url);
}
?>

 

Then whichever string you want to replace %26 with &, just do this:

 

<?php
echo replace_26_percent('http://www.url.com?a=a%26a=a');
?>

Link to comment
Share on other sites

could you show the script?

 

I follow your question, although the answers you're getting are kinda leading FROM a request, not TO a request, basically the responses here are telling you how to manage the query string in the script file itself, not to prevent the %26 in the first place..

 

replacing %26 could potentially cause errors as far as url encoding is concerned..

 

if you did str_replace('%26','&',$query);

 

an there is a & in the request which SHOULD be url encoded, you will have a malformed request when you try to explode it by & and you will find yourself bald with plenty scratch-induced cuts in your beard..

 

so the best method to solve this problem is to correct the issue instead of working around the issue, and I'm more than certain there is something in your code which is causing the issue :)

Link to comment
Share on other sites

I had a quick try with the str_replace and couldnt get it working, probably something im doing though.

 

The initial request is not coming from a PHP script that I have control of, but here is the script that is recieving the URL:

 

<?

    
  $name = $_GET['name'];
  $myuid = $_GET['myuid'];

  $service = "localhost";
  $username = "";
  $password = "";
  $database = "";


mysql_connect($service, $username, $password);
@mysql_select_db($database) or die( "Unable to select database");

$query = "SELECT * FROM scores WHERE mode = 0 ORDER BY score DESC";
$result = mysql_query($query);
$num = mysql_numrows($result);

$loopindex = 0;
$found = false;

while ($loopindex < $num && $loopindex < 25) {

$thisname = mysql_result($result, $loopindex, name);
$thisscore = mysql_result($result, $loopindex, score);
$thisuid = mysql_result($result, $loopindex, uid);


    if ($thisname == $name && $thisuid == $myuid){
	$found = true;
echo ("$loopindex $thisscore $name\n");
}else{
	if($loopindex == $num - 1 && $found == false){
		echo ("00 No Score");
	}
}

$loopindex++;
}

?>

 

Hope that helps...

 

Link to comment
Share on other sites

Ive tried using & in the URL and that fails too.

 

Ive added the below to the top of the script that recives the URL:

 

$query = $_SERVER["QUERY_STRING"];

$stripped = str_replace('%26', '&', $query);
echo ("$stripped\n");

 

That does echo the URL as it should be with the %26's replaced with &'s..but the rest of the script fails.

Link to comment
Share on other sites

The SQL query didnt return the expected result, im assuming because it was using the GET variables?

 

Ive found a way around it using the below:

 

$query = $_SERVER["QUERY_STRING"];

$array = explode("|",$query);

$array[0]; 
$array[1];   
  
$name = $array[0];
$myuid = $array[1];

 

Now if I replace the & in the URL with a | it isnt encoded and the query works fine :)

 

Are there any down sides to doing it this way?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.