cURL with PHP question for logging in to https:// remote site - php

So I have this cURL script for remote login. It works fine for some pages but not the pages I need.
For the page that isn't accessible through the script, the remote server requires the url be like this:
https://sub.example.com/a/b/thisPage.aspx?aVar=%2Fa%2Fb%2Fc%2Fd%2FFile+Name.nev
It seems that cURL or just php automatically converts the last part to
... thisPage.aspx?aVar=/a/b/c/d/File+Name.nev
I have php echo out the url variable just before it is passed to cURL and the last part is:
...thisPage.aspx?aVar=%2Fa%2Fb%2Fc%2Fd%2FFile+Name.nev
but it gives an error
message "Bad Request" and the browser url shows:
...thisPage.aspx?aVar=/a/b/c/d/File+Name.nev
When I manually enter enter ... thisPage.aspx?aVar=%2Fa%2Fb%2Fc%2Fd%2FFile+Name.nev in my browser it pulls up the page just fine.

Try double encoding so that way the % are encoded to. Try replacing them with %25.

Related

file_get_contents against symfony2

I have a file_get_contents($myUrl) call from a flat PHP script that isn't working.
If I run $myUrl in a browser it works fine, if I do it over the file_get_contents() it behaves as if the url is incorrect or incomplete.
The myUrl looks like this
https://login.myApp.com/getWifiSettings/ofBoSf593f
Where the last part is a token, and the sym2 webApp behaves as if that token is incorrect, the same way as if I were to paste everything but the last character in the browser (thus producing an incorrect token).
I don't know whether this is an issue caused by the file_get_contents() (do I need any parameters with it to work?) or if it is some security setting in my sym2 installation that denies access for such a call (how does it distinguish between a user's web browser calling the route and a script using file_get_contents to access the route?)
Try to us trim() on the string before you submit it to file_get_contents. It may be that a whitespace character is in the variable and is being submitted and interpreted as part of the URL.

How could I use PHP to get page by URL, which redirects to local path?

I'm trying to get page content by URL (e.g. 'http://example.com/page.html') using PHP, but remote server redirects it to another page (e.g. 'http://example.com/another-page.html') and when I try to execute code:
$p = file_get_contents('http://example.com/page.html');
echo $p;
browser returns 404 error:
The requested URL /another-page.html was not found on this server.
As I understand, instead of getting 'http://example.com/another-page.html', PHP tries to return 'another-page.html' from my localhost and of course there is no such page. Seems like remote server redirects using local address ('/another-page.html' instead of 'http://example.com/another-page.html') which is correct of course, but I don't know how to deal with it.
Could anybody advise me, how could I solve this problem?
Thanks in advance

Remote login PHP

I am having issues with the code to access a remote website server2 from another website server1. The code I use from inside the server2 to log in is the following code:
require_once("http://server2.com/access/models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
//Prevent the user visiting the logged in page if he/she is already logged in
if(isUserLoggedIn()) { header("Location: http://server2.com/access/account.php"); die(); }
On server2 it looks like I can not use require_once() because the page does not pass and when I use include() then prevent the user visiting does not pass. I think I am missing a code like cURL or path. Can anybody post the code to call my server2 from a another remote server1?
You can't include/require PHP pages from another server because those php-files are getting parsed. You will end up getting the parsed HTML code or non at all if your config.php meets minimum safety standards. You'd have to open it differently but be aware that if you can open config.php in an easy way remotely, everyone else can too...
I suggest not to use include at all but try a different approach: for example via a php script on that second server that returns you plain text like:
login|logout
ok|error
id_Paul23
pwHash_asdf02302afbd33
the second items after the | represent an alternative response if you have an erroneus login attempt. Your user sends the login-data to your script on server 1 which attempts a login-try on that special script on server 2 which returns the result above and you can then do whatever you want on server 1.
Or better: send/receive the data in a standardized format like JSON. You already have functions in PHP for that: json_encode() and json_decode() to parse your data.
Here is an example on how you could do this on a larger scale: http://www.jasny.net/articles/simple-single-sign-on-for-php/
This article will help you send the request to server2: How do I send a POST request with PHP?

Using PHP inside Javascript? To shrink URL

I made a bitly url shrinker, and I currently have a Soundcloud Javascript API that outputs a url link of a song. Im trying to shrink it using my shrinker. The shrinker works using this:
<?php echo $bitly->shorten('http://google.com'); ?> //Equals google.com in short url format
The javascript code I'm trying to implement it in is this: Ill go ahead and give you what I tried to do already, that didn't work.
Before I edited:
container.find('span.player-actions').html(
'Soundcloud | Download'
);
After I tried:
container.find('span.player-actions').html(
'Soundcloud | Download'
);
Any suggestions, I'm open to anything. And would love to make this work!
That has been already explained but in case you're new to this concept, there is a simplified explanation.
<?php tags in your code are processed on server before your page is sent to user's browser. Actually browser never receives those tags - they're replaced with PHP output on server and then the resulting page is sent to user.
As a result of some mistake sometimes PHP code makes into user's browser but it behaves as any other non-standard tag - content between <?php and ?> would be invisible to visitor.
JavaScript, on the other hand, operates in user browser with (in our case) what PHP has already output. When you change the page with JavaScript, it's not sent back to server - actually, server is totally unaware of that, so it can't execute the PHP code you're outputting by your JavaScript.
In order to achieve a similar result you need to send an AJAX request from your JavaScript code. It'll basically be another "page request" initiated by your JavaScript, but happening at the background with PHP output not replacing your current page, but arriving into your JavaScript code. This way your JavaScript is outputting PHP output and not PHP code, that's why it is possible.
You cannot call PHP on a string that is generated via javascript since PHP is server side and executed before JavaScript which is client side.
If you want to shorten this string, you'll have to make an ajax call to a php page that will return the shrunk url.

Blank PHP/Index Page on Android HTTP Post Request

Upon using the code samples below, I try to send a HTTP request to validate a username and password entry to a PHP script (returning either 1 or 0 in an echo).
Using HTTP Assistant, testing the HTTP Post request has the expected results... But for some reason, when logging the 'res' String (the HTTP response) in the java code, I get a blank PHP/Index page:
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML3.2Final//EN"><html><title>Indexof/</title></head><body><h1>Indexof/</h1><ul><li>cgi-bin/</li></ul></body></html>
Code: HomeActivity.java and Http.java
Have I done something wrong code-wise? Or is this a server issue?
What you are seeing there is the standard webserver listing of a directory. So you probably have the wrong URL you're hitting. Is there any redirect magic involved?
[edit] As you have controll of the PHP page yourself, do the following: Edit it so that it accepts parameters per GET and try to call the page via your android browser with the username and password as GET parameters . If that works, you've at least a clue that it's possible from your phone.

Categories