insert an URL in mysql database using php script with GET method - php

http://192.168.1.7/cars_store/insert_annoce.php?urlFace=firebasestorage.googleapis.com/v0/b/carsstore-1c8c5.appspot.com/o/photos%2Fimage%3A60Sat+Dec+24+10%3A39%3A10+EST+2016?alt=media&token=908e383e-1901-4c04-855d-d6c7280b40a1
$urlFace=$_GET['urlFace'];
echo $urlFace;
and this is the result of the script:
firebasestorage.googleapis.com/v0/b/carsstore-1c8c5.appspot.com/o/photos/image:60Sat Dec 24 10:39:10 EST 2016?alt=media
I always get the url missing the last part (after '&token=' ) method

Since you're passing the URL as a GET parameter within another URL, you'll have to somehow escape it so that it is distinguishable from the url that contains it.
an easy solution would be to use POST, where the data doesn't get sent as part of the URL, and therefore doesn't have this problem.
Otherwise, you'll have to encode your URI within a URI: http://php.net/rawurlencode

You can use urlencode()
This function is convenient when encoding a string to be used in a query part of a URL, as a convenient way to pass variables to the next page
Eg:
$my_url = "http://192.168.1.7/cars_store/insert_annoce.php?
urlFace=".urlencode("firebasestorage.googleapis.com/v0/b/carsstore-
1c8c5.appspot.com/o/photos%2Fimage%3A60Sat+Dec+24+10%3A39%3A10+EST+2016?
alt=media&token=908e383e-1901-4c04-855d-d6c7280b40a1");
You can use $my_url in <a> tags , form actions etc.
Then $_GET[urlFace] in annoce.php will give the full url you needed

Related

How to remove any given $_GET variable from URL with PHP?

I'm puttings filters in links with GET variables like this: http://example.com/list?size=3&color=7 and I'd like to remove any given filter parameter from URL whenever a different value for that particular filter is selected so that it doesn't, for example, repeat the color filter like so:
http://example.com/list?size=3&color=7&color=1
How can I if(isset($_GET['color'])) { removeGet('color'); } ?
You can use parse_url and parse_str to extract parameters like in example below:
$href = 'http://example.com/list?size=3&color=7';
$query = parse_url( $href, PHP_URL_QUERY );
parse_str( $query, $params );
// set custom paramerets
$params['color'] = 1;
// build query string
$query = http_build_query( $params );
// build url
echo explode( '?', $href )[0] . '?' . $query;
In this example explode() is used to extract the part of the url before the query string, and http_build_query to generate query string, you can also use PECL http_build_url() function, if you cannot use PECL use alternative like in this question.
You can't remove variables from GET request, just redirect to address without this var.
if (isset($_GET['color'])) {
header ('Location: http://www.example.com/list?size=' . $_GET['size']);
exit;
}
Note: in URL http://example.com/list?size=3&color=7&color=1 is just one $_GET['color'], not two. Only one of them is taken. You can check, is $_GET['key'] exists, but you don't know how many of them you have in your URL
So, assuming I'm understanding your question correctly.
Your situation is as follows:
- You are building URLs which you put into a webpage as a link ( <a href= )
- You are using the GET syntax/markup (URL?key=value&anotherkey=anothervalue) as a way to assign filters of some sort which the user then receives when they click on a given link
What you want is to be able to modify one of the items in your GET parameter list (http://example.com/list?size=3&color=7&color=1) so you have only one filter key but you can modify the filter value. So instead of the above you would start with: (http://example.com/list?size=3&color=7) but after changing the color 'filter' you would instead have http://example.com/list?size=3&color=1).
Additionally you want to do the above in PHP, (as opposed to JavaScript etc...).
There are a lot of ways to implement the change and the most effective way to do it depends on what you are already doing, most likely.
First, if you are dynamically producing the HTML markup which includes the links with the filter text, (which is what it sounds like), then it makes the most sense to create a PHP array to hold your GET parameters, then write a function that would turn those parameters into the GET string.
New filters would appear when a user refreshed the page, (because, if you are dynamically producing the HTML then a server request is required to rebuild the page).
IF, however, you want to update the link URLs on a live page WITHOUT a reload look into doing it with JavaScript, it will make your life easier.
NOTE: It is likely possible to modify the page, assuming the links are hard coded, & the page is hard coded markup, by opening the page as a file in PHP & making the appropriate change. It's my opinion that this would be a headache and not worth the time & effort AND it would still require a page reload (which you could NOT trigger yourself).
Summary
If you are writing dynamic pages with PHP it shouldn't be a big deal, just create a structure (class or array) and a method/function to write that structure out as a GET string. The structure could then be modified according to your desire before generating the page.
If, however, you are dealing with a static page, I recommend JavaScript (either creating js structures to allow a user to dynamically select filters or utilizing AJAX to build new GET parameter lists with PHP and send that back to the javascript).
(NOTE: I am reminded that I have done something along the lines of modifying links on-the-fly for existing pages by intercepting them before they are displayed to the user [using PHP] but my hands were tied in other areas and I would not recommend it if you have a choice AND it should be noted that this still required a reload...)
Try doing something like this in your back-end script:
$originalValues=array();
foreach($_GET as $filter=>$value)
{
if(empty($originalValues[$filter]))
$originalValues[$filter] = $value;
}
This may do what you want, but it feels hackish. You may want to revise your logic.
Good luck!
just put a link/button send the user to index... like this.
<a class="btn btn-primary m-1" href="http:yoururl/index.php" role="button">Limpar</a>

Using multiple nested _GET variables in a single URL

Setup:
Script that generates word images from multiple letter images
(autotext.php)
URL is formatted:
www.whatever.com/autotext.php?text=hello%20world
Script that alters images server-side to run filters or generate
smaller sizes (thumbnail.php)
URL is formatted:
www.whatever.com/thumbnail.php?src=whatever.png&h=XXX&w=XXX
Use-case:
I want to generate a smaller version of the autotext server-side. So my call would look something like:
www.whatever.com/thumbnail.php?src=autotext.php?text=hello%20world&h=XXX&w=XXX
As you can see, I would like to treat a URL with _GET variables as a variable itself. No amount of playing with URI encoding has helped make this work.
I have access to the PHP for both scripts, and can make some simple alterations if that's the only solution. Any help or advice would be appreciated. I would not even rule out a Javascript frontend solution, though my preference is to utilize the two scripts I already have implemented.
You should be able to do this by urlencoding all the $_GET params into a variable then assigning that variable to another, like this (untested):
// Url generation
$url = www.whatever.com/thumbnail.php?src=(urlencode(http_build_query($_GET)));
Then you should be able to retrieve on other side:
$src = urldecode(explode('&', $_GET['src']));
I've seen this exact behavior when trapping where to redirect a user, after an action occurs.
---- Update ----
Your "use case" url was correct:
www.whatever.com/thumbnail.php?src=autotext.php?text=hello%20world&h=XXX&w=XXX
.... except that you CANNOT have more than one ? within a "valid" url. So if you convert the 2nd ? to a &, you should then be able to access $_GET['text'] from the autotext.php script, then you can urldecode it to get the contents.

Strange Error with Complex PHP Form Processing

NEW INFORMATION:
I used the print_r function on the $_REQUEST and something very strange is happening there too. Some values are being correctly passed by the GET such as a value on another form which passes in "TRUE". This can be seen in the print_r output but isn't written to the file... Still no closer to finding a solution to my problem however.
I'm working on a page with a lot of forms which are loaded in as needed by AJAX. This all works fine as does parsing the name:value pairs and storing them appropriately.
My error happens when the PHP parses the GET request sent by AJAX when the user is finished, it only seems to retrieve the values from certain fields.
The idea is that the user can add data from any number of forms, which are then turned into a GET request and sent to the server.
The JavaScript is building my request perfectly and all forms are sent correctly.
Depending on the forms the user submits, the data is processed by a large switch statement which passes the relevant names to a variadic function which grabs the values, and creates a string for writing to a file.
The strange error is that only some values get written to the file with others only having a blank line. No error reported by Apache or PHP, no error reported in the JavaScript console either.
I'll use the Colour form for example as this is one of the more complex.
So I add a colour action and click the button to submit all forms (this time, it's just the colour form though)
My get request looks like this:
actionIDs=Colour&coOptionSelect=Tinting&coColourEffect=Sepia&coRemoveColour=#000000&coRemoveFuzzNumber=0&coRemoveHueSelect=None&coReplaceColour=#000000&coReplaceFuzzNumber=0&coReplacementColour=#000000&coReplacementAlphaNumber=0&coReplaceHueSelect=None&coReplacementHueSelect=None
Next, the PHP parses the actionIDs part as sometimes, there will be many actions. This works fine.
We now jump to the Colour part of the switch statement.
case "Colour":
$config = processAction("coOptionSelect", "coColourEffect", "coRemoveColour", "coRemoveFuzzNumber", "coRemoveHueSelect", "coReplaceColour", "coReplaceFuzzNumber", "coReplacementColour", "coReplacementAlphaNumber", "coReplaceHueSelect", "coReplacementHueSelect");
file_put_contents($confpath . "colour.conf", $config);
break;
That writes to the correct file, but strangely, only coOptionsSelect and coColourEffect have their values written to the file. It isn't their input type as they are select statements similar to the other selects on the form. On other forms, it may be a number input or a text input that submits properly instead.
It isn't random either, the same ones will always write out properly. It also isn't positional as I moved around the values and it's still the same ones that write correctly, their position doesn't affect anything.
Finally here is processAction function.
function processAction()
{
$config = "";
foreach(func_get_args() as $field)
{
$temp = isset($_REQUEST[$field]) ? $_REQUEST[$field] : null;
$config = $config . $temp . "\n";
}
return $config;
}
The end result should be all values should write to their relevant files correctly, rather than the current issue where only a few values from each form are written, with the rest of the values being written as blank lines.
You probably need to encode your # sign to a encoded method %23
you could also use urlencode to do it before passing it to your variable.
Reference: http://php.net/manual/en/function.urlencode.php
Update:
If you are going to try to encode through javascript I would try and use this method
var newURL =
"http://example.com/index.php?url=" + encodeURIComponent(actionIDs);
or
var newURL =
"http://example.com/index.php?url=" + escape(actionIDs);
Reference: Encode URL in JavaScript?
You have three options:
escape() will not encode: #*/+
encodeURI() will not encode: ~!##$&*()=:/,;?+'
encodeURIComponent() will not encode: ~!*()'
But in your case, if you want to pass a URL into a GET parameter of other page, you should use escape or encodeURIComponent, but not encodeURI.
See Stack Overflow question Best practice: escape, or encodeURI / encodeURIComponent for further discussion.

Retrieve content of another website

I want to get the content of another page. The background is that I wanted to make an AJAX request but due to the Same Origin Policy I cannot do this. Now I wanted to write an own PHP script on which I make the AJAX request. The URL looks like the following:
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332
I tried it with fopen, curl and file_get_contents. Nothing from the works. The problem is if I put in the URL as string like
$results = file_get_contents('http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332');
it does work. If I put in a variable
$url = 'http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=1&tx_manager_pi9[category]=test&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&cHash=123456789001233455332';
$results = file_get_contents($url);
I come to a wrong page. With the specific parameter I get a result. If the parameter are not given correctly it seems that I come to a default page. I can't make a sense out of it.
The same for curl:
$curlSession = curl_init();
$options = array
(
CURLOPT_URL=>$url,
CURLOPT_HEADER=>false,
CURLOPT_RETURNTRANSFER=>true,
CURLOPT_FOLLOWLOCATION=>true
);
curl_setopt_array($curlSession,$options);
$results = curl_exec($curlSession);
This doesn't work. If I put in the URL as string and not as variable I get some results! I thought that the ampersand & or the square brackets [] are the problem but I cannot say this. & should be reserved and [] are no correct URL parameters. But why does the direct input work and not the variable?
I used the variable because I make some replacements with str_replace where I make the query more flexible.
I saw similar questions here (cURL function not working, curl_setopt doesnt work with url as a variable) but there was never posted a real solution.
You have a , instead of a ; in your second code block.
Are you required to be "logged in" to the site that you're visiting? That would explain why it's working in your browser and not through your server script.
If all else is the same, your browser and the PHP functions you listed should return the same results.
Could you provide the actual URL for us to test?
EDIT: Based on the URL you provided, it's working fine for me:
php > $test = file_get_contents("http://www.domain.com/user/user_neu/index.php?id=16518&tx_stusermanager_pi9%5Bindications%5D=1&tx_stusermanager_pi9%5Bcategory%5D=cure&tx_stusermanager_pi9%5Baction%5D=getHousesByIndications&tx_stusermanager_pi9%5Bcontroller%5D=HouseFinder&cHash=88230660f01ads34d73a199b82e976");
php > var_dump($test);
string(29) "16,15,14,13,12,11,17,19,22"
My problem was that I used an encoded URL as starting point. E.g.
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9%5Bparameter%5D=%23%23%23param1%23%23%23&tx_manager_pi9%5Bcategory%5D=%23%23%23param2%23%23%23&tx_manager_pi9%5Baction%5D=getInfos&tx_manager_pi9%5Bcontroller%5D=Finder&cHash=123456789001233455332
I made a str_replace on a URL encoded string. Even using urldecode afterwards the URL was not correctly generated for curl, file_get_contents, ...
The correct URL should be something like this
http://domain.com/subfolder/another_subfolder/index.php?id=1234&tx_manager_pi9[parameter]=###param1###&tx_manager_pi9[category]=###param2###&&tx_manager_pi9[action]=getInfos&tx_manager_pi9[controller]=Finder&&cHash=123456789001233455332
i.e. without &, %23, %5B, %5D

AJAX & PHP:GET parameters causing Flickr api key error

I'm trying to send a URL with aFLickr API key to fetch results for a given photo tag. The Ajax code should return the XML to my browser. However the URL structure with parameters seems to cause a problem in my setup:
**the HTML file:**
...
url="api.flickr.com/services/rest/?method=flickr.photos.search&api_key=75564008a468bf8a284dc94bbd176dd8&tags=paris"
request.open("GET","xmlget.php?url=" + url + nocache, true)
...
**the 'xmlget.php' file:**
...
echo file_get_contents($_GET['url']);
...
error: code="100" msg="Invalid API Key (Key has invalid format)">
the link works fine if tested in the adress bar so there must be a breakdown somewhere when the URL is processed.
i tried wrapping it into encodeURI but no luck :(
Note: related post
You need to use encodeURIComponent instead of encodeURI to actually get that string encoded.
May I make 2 suggestions?
just pass the search parameters to xmlget.php and do the rest there even if it means having to pass a service type if you are using that generically
I don't remember what all a Flickr api key gets you, but it's generally a bad thing to post anything called an "api key" in public. In addition to the question, that includes sticking it in javascript that an end user can access.

Categories