Getting URL variable that is a string instead of a number - php

I have a PHP script which I can query with a variable.
Here is the URL to do that:
ajax.php?var=3
And here is the bit (inside the PHP file) that grabs that number - it's a phpBB method that works similarly to GET()/POST():
$alink = $request->variable('var', 1);
echo $alink;
Here's the API section about this 'request' method if interested.
And this is what is outputted on the page when visited:
3
So that works fine. However, when I used a string instead of a number, all it returns is 0 :(
ajax.php?var=Apple
Returns:
0
When visited... so like, why is this and what do I have to do to get my request method to recognised the string? It works fine with a number, so why not a string too?
Thanks :)

Related

curl_exec pulling wrong HTML vs string passed to curl_init

My curl request string is created programmatically with a base URL from a database and an integer which is the page number. The string for the URL is correct. If I call the curl request with the base URL copied in to the function, it works. If I call it with the programmatically created string (which is identical) the wrong page is called - it appears to be missing one of the parameters from the string (affiliation):
This has been running for years with no issue. The server is PHP 5.6. The string in the database is stored as varchar(500). I have tried TEXT as the type in the database. I have tried various ways of forcing a string in the function. The first $testurl below is copied and pasted from the second $testurl if I get the code to echo it before making the curl call.
//choose one of the $testurls to try
$testurl = 'https://log.concept2.com/challenges/holiday/2019/honorboard/200?country=0&state=&affiliation=1397&sort=distance&page=2';
//In this... $url comes from the database, $gotopage comes from a loop
$testurl = (string)($url . $gotopage);
$c = curl_init($testurl)
$str = curl_exec($c);
curl_close($c);
echo $str;
die;
The first $testurl returns what I want, a list of people in the same team.
The second $testurl returns a list of people belonging to any affiliation.
Edit
var_dump($testurl) gives:
string(134) "https://log.concept2.com/challenges/holiday/2019/honorboard/200?country=0&state=&affiliation=1397&sort=distance&page=2"
The full code is more of a challenge. The $GoToPage is an integer. $url is pulled from a database. The pertinent line is:
$url = $challenge['mb2_Challenges']['challenge_url'];
Where the $challenge is created from a mysqli SELECT... FROM... WHEN... query with SQL. mb2_Challenges is the table, challenge_url is the table.
For those who are as foolish as me, here is the answer:
The string from the script was urlencoded, so & instead of &. I was in too much of a rush to spot this. It just needed htmlspecialchars_decode.
Lesson learned: always look at the source!

Convert &#038 to &

Ho you all, I've got a script in a Wordpress post that sends the value of 4 variable to a URL.
The fact is that since natively WordPress converts & to &#038, the URL that is meant to recive those variable cannot get them, since the final URL will be
http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4
instead of http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4
Now I know that it is possible to fix this problem by commenting to lines in wp-includes/formatting.php, but I'm looking for a PHP function that can convert the URL with '&#038' to an URL with just '&'.
Is it possible? Thanks!
You will need to use htmlspecialchars_decode(). Consider this example:
$url = 'http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4';
$url = htmlspecialchars_decode($url);
echo $url;
// http://localhost/php/add.php?a=VALUE1&b=VALUE2&c=VALUE3&d=VALUE4

Search for part of a string in array

i have been over so many posts online trying to sort this....
i am integrating a small search feature into a php script....
i have tried many scripts including in_array (only works for full match) and array_search
and nothing seems to work....
i have a script that creates an array with file names (all in the format "Random-Name-1.ext")
but the script removes the file extention, just leaving the filenames....
all filenames are seperated word by word with a -
all words have the first letter capital....
the array is called $files1;
the search string is called $search_string;
what im looking for would be along the lines of a foreach loop to check if the search string is contained in any part of each array value, and if it is, put the full array value into another array called $search_results
as the next part of my script to paginate needs the array $search_results to echo each of the filenames and display them 10 per page....
hope this is enough info, ive been workin on it for ages and racking my brains trying to find the correct code....
thanks in advance
..........
...........
EDIT.....
.......
........
got script working with preg_grep..... but i now have a slight problem....
the script before the search code is designed to get the url of the search page with q={search string}
and then trim this so that i just have the serach string as a variable.....
i use an str_replace to change the page url from
/games-search?q={search-string}
to {search-string}
this is perfectly fine, but the script to paginate the results as 10 per page adds ?page=2 to the url....
so when i click page 2, the str_replace to change the url to search string doesnt work now, as the new page url for page 2 is
/games-search?page=2&q={search-string}
i have been trying to do another str_replace to change the consecutive pages urls to just the search string but i am having problems with the regex to define the page number.....
the page numbers vary from 1 - about 50 (never more than 99, so 2 digits would be enough to match....
i have tried over and over again today to get this regex correct but i am not sure if i am going about it the correct way.....
here is my latest effort....
/games-search?page=(^[0-9][0-9]+)&
that is what i am trying to replace with "" (eg, nothing)
as i only need the data from after the & character in the url ... and thats IF the url even contains this (for exapmple the first page when the url doesnt contain the & character - if the url doesnt contain the & character , i dont want it modifying as i already have the data i need)
thanks again if anyone can help
decided not to paginate the results of each search as ther will never be more than about 15 results..... this is fine, but i am going to implement a miminum search length of 4 characters so it doesnt bring up 500 results for the letter A :)
Search an array for a substring match? preg_grep
Then do something like
$search_results = ...;
$paginated = array();
for ($i = 0; $i < count($search_results); $i++)
{
if ($i % 10 == 0) $paginated[$i + 1] = array();
$paginated[$i][] = $search_results[$i + 1];
}
Then print_r that array, and you'll see you've got something dead easy to work with.

Jquery, ajax and the ampersand conundrum

I know that I should encodeURI any url passed to anything else, because I read this:
http://www.digitalbart.com/jquery-and-urlencode/
I want to share the current time of the current track I am listening to.
So I installed the excellent yoururls shortener.
And I have a bit of code that puts all the bits together, and makes the following:
track=2&time=967
As I don't want everyone seeing my private key, I have a little php file which takes the input, and appends the following, so it looks like this:
http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=http://urltoshorten?track=2&time=967
So in the main page, I call the jquery of $("div.shorturl").load(loadall);
It then does a little bit of CURL and then shortener returns a nice short URL.
Like this:
$myurl='http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=' . $theurl;
$ch = curl_init($myurl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if ($data === false) {
echo 'cURL failed';
exit;
}
echo $data;
All perfect.
Except... the URL which is shortened is always in the form of http://urltoshorten?track=2 - anything after the ampersand is shortened.
I have tried wrapping the whole URL in php's URLencode, I've wrapped the track=2&time=967 in both encodeURI and encodeURIComponent, I've evem tried wrapping the whole thing in one or both.
And still, the & breaks it, even though I can see the submitted url looks like track=1%26time%3D5 at the end.
If I paste this or even the "plain" version with the unencoded url either into the yoururls interface, or submit it to the yoururls via the api as a normal URL pasted into the location bar of the browser, again it works perfectly.
So it's not yoururls at fault, it seems like the url is being encoded properly, the only thing I can think of is CURL possibly?
Now at this point you might be thinking "why not replace the & with a * and then convert it back again?".
OK, so when the url is expanded, I get the values from
var track = $.getUrlVar('track');
var time = $.getUrlVar('time');
so I COULD lose the time var, then do a bit of finding on where the * is in track and then assume the rest of anything after * is the time, but it's a bit ugly, and more to the point, it's not really the correct way to do things.
If anyone could help me, it would be appreciated.
I have tried wrapping the whole URL in php's URLencode
That is indeed what you have to do (assuming by ‘URL’ you mean inner URL being passed as a component of the outer URL). Any time you put a value in a URL component, you need to URL-encode, whether the value you're setting is a URL or not.
$myurl='http://...?...&url='.rawurlencode($theurl);
(urlencode() is OK for query parameters like this, but rawurlencode() is also OK for path parts, so unless you really need spaces to look slightly prettier [+ vs %20], I'd go for rawurlencode() by default.)
This will give you a final URL like:
http://myshorten.example/yourls-api.php?signature=x&action=shorturl&format=simple&url=http%3A%2F%2Furltoshorten%3Ftrack%3D2%26time%3D967
Which you should be able to verify works. If it doesn't, there is something wrong with yourls-api.php.
I have tried wrapping the whole URL in php's URLencode, I've wrapped the track=2&time=967 in both encodeURI and encodeURIComponent, I've evem tried wrapping the whole thing in one or both. And still, the & breaks it, even though I can see the submitted url looks like track=1%26time%3D5 at the end.
Maybe an explanation of how HTTP variables work will help you out.
If I'm getting a page with the following variables and values:
var1 = Bruce Oxford
var2 = Brandy&Wine
var3 = ➋➌➔ (unicode chars)
We uri-encode the var name and the value of the var, ie:
var1 = Bruce+Oxford
var2 = Brandy%26Wine
var3 = %E2%9E%8B%E2%9E%8C%E2%9E%94
What we are not doing is encoding the delimiting charecters, so what the request data will look like for the above is:
?var1=Bruce+Oxford&var2=Brandy%26Wine&var3=%E2%9E%8B%E2%9E%8C%E2%9E%94
Rather than:
%3Fvar1%3DBruce+Oxford%26var2%3DBrandy%26Wine%26var3%3D%E2%9E%8B%E2%9E%8C%E2%9E%94
Which is of course just gibberish.

Javascript syntax for passing PHP variables

HELLO EVERYONE :D
I am having some diffculties passing php varaibles through javascript.
Basically this is the statement:
new Ajax.Updater( 'result', 'update_request.php?status='+status_change);
and I need to pass another variable after status_change, the variable I need to pass is the id of the ticket that is being updated.
So I tried this:
new Ajax.Updater( 'result', 'update_request.php?status='+status_change'&requestid='+request_id);
However this gives me an error stated below:
Webpage error details
Message: Expected ')' Line: 128 Char:
77 Code: 0 URI:
http://site_url/dev/time_off_new/main.php
I have tried double quotes, as well as a combination. I do not know why it is giving me such harrassment.
I ran the php script that it is attached to, and everything is working fine. After debugging I have narrowed it down to this statement, so everthing else is working fine.
Thank you ahead of time.
Peace and love!
You are missing a concatenation operator after the variable status_change
Try this
new Ajax.Updater( 'result', 'update_request.php?status='+status_change+'&requestid='+request_id);

Categories