First off, I'm a complete novice as a web developer. I have a PHP function that handles a post request for HTTP, and it works great. I read a few places online that all I have to do to make that same function post to HTTPS is change the port I'm hitting from port 80 to port 443. So instead of looking like this:
$fp = fsockopen($host, 80, $errno, $errstr, 30);
It would look like this:
$fp = fsockopen($host, 443, $errno, $errstr, 30);
Unfortunately, this change doesn't seem to be working. So my questions are these:
Is it true that all I have to change is the port number?
If there is more to do, than what is it I still need to do?
Please try to keep things in as simple terms as possible, since I am the first to admit I'm very new to this kind of stuff.
Thanks a ton everyone.
Is it true that all I have to change is the port number?
No
If there is more to do, than what is it I still need to do?
You have to negotiate an SSL connection and tunnel the HTTP request through it.
Don't try to do this with sockets. Use a library designed for it, such as cURL.
From php.net:
If OpenSSL support is installed, you may prefix the hostname with either ssl:// or tls:// to use an SSL or TLS client connection over TCP/IP to connect to the remote host.
Try prepending ssl:// to your $host (but also keeping port 443;
Related
I'm currently implementing LDAP Authentication. I cache the credentials as a fallback in case the LDAP server is offline. As part of this caching I need to check if my LDAP server is online. Rather than using PHP's Ldap methods it would be better to use something simple like a ping.
Please note that it should be able to handle any protocols. E.g., I can't use fsockopen because it does not support ldaps://. [I know that I could register my own protocol wrappers].
I want this check to be generic and simple.
I'm using fsockopen for exactly that purpose. It doesn't matter whether it supports ldaps or not I figured out, because there are two possibilities in the end:
The appropriate port is open, so I can assume that the LDAP-Server is up and running or
The appropriate port is not open, so I can assume that the LDAP-Server is not running.
You can check that like this:
$fp = #fsockopen($host, $port, $errno, $errstr, 30);
if (! $fp) {
// Port is unavailable
}
fclose($fp);
Now you know, the port to connect to is open and I can fire up LDAP.
I've found two edge-cases that you won't be able to check for using this method though
The LDAP-Server is in an undefined state and has the port still open but is not responding or
Some other application has opened the port.
You can check that though by using
$con = ldap_connect($ldapURI);
if (! ldap_bind($con, $user, $password)) {
// Something is fishy
}
Fishy might be invalid credentials (which should not happen at this first bind, right?) or the server listening on that port is not responding in a manner that we expect. So it's either not an LDAP-Server or the server is in an undefined state.
To fail fast, you should adapt the timeouts appropriately so you're not waiting half a minute just to know that something went wrong.
YOu can set the timeout for fsockopen using the fifth parameter and you can set the timeouts for LDAP using
ldap_set_option($con, LDAP_OPT_NETWORK_TIMEOUT, [whatever is appropriate]);
ldap_set_option($con, LDAP_OPT_TIMEOUT, [whatever is appropriate]);
ldap_set_option($con, LDAP_OPT_TIMELIMIT, [whatever is appropriate]);
// Only available when your LDAP-extension is compiled against the Netscape LDAP C-SDK
ldap_set_option($con, LDAP_X_OPT_CONNECT_TIMEOUT, [whatever is appropriate]);
You'll need to set them after ldap_connect but before ldap_bind.
LDAP_OPT_TIMEOUT and LDAP_X_OPT_CONNECT_TIMEOUT are not (yet) documented on php.net though!
For more infos on these constants have a look at https://linux.die.net/man/3/ldap_set_option but beware that not all the constants mentioned there are implemented in the PHP-LDAP-Extension.
Let's say i have this proxy: 222.74.98.234
Ports : 80, 8080, 443, 1080 (http and stock4/5 ports)
Now i'm trying to connect by using this proxy with a list of those ports to see if each port connection was successful or not.
To give me output to be something like that:
222.74.98.234:80 - ok
222.74.98.234:8080 - bad
222.74.98.234:443 - ok
222.74.98.234:1080 - bad
I have tried almost of every answers which i found here, and currently i'm using this codes
But this not exactly what i want, i tried with fsockopen and socket but they slow and i don't have enough experience with curl
Could anyone help me please?
Thanks
So Im trying to get my head wrapped around this....
I open the port
$remip = $_SERVER['SERVER_ADDR']; //Grab my server address
$fp = fsockopen($remip, 80, $errno, $errstr, 10);//Godaddy hosting only 80 and 443 ports work
//fsockopen(ip address , port, IDK, IDK, timeout delay)
so now the ports open or if not maybe some error checking to be sure
if (!$fp) { echo "$errstr ($errno)<br>\n"; exit; } //Not sure what this echos out but its clear how it stops errors
So now that the port is open any ip/client can connect on this port????
Ill assume I can now connect....
So on my client I open a socket to my server ip address port tcp connection.....
The php file includes something like
else {$out = "hello, 80\r\n"; //out specifies the string to be written , bytes to write
fwrite($fp, $out); //$fp is the handle
fclose($fp)}//close the connection
at this point ill assume that my client gets the hello written to it ..
finish up by closing the connection
Im entirely new to this so Im attempting to understand some sample code here...
So how long is this socket open for? If i want to keep this port open do i need to do a cron job to launch this file periodically.
Im 100% sure that I have got something wrong here so please set me straight.
I think you have a misconception of what fsockopen does. In your example your fsockopen does not actually open port 80 (as in opening a server socket), but it opens a client socket that connects to port 80 on the server itself. It actually does open a (client) port which gets a (not completely) random number.
After you connected using fsockopen you can send HTTP commands to the webserver such as GET /index.php
What you need to use is socket_listen() and socket_bind(). There are a few places in the docs that show you how to get PHP listening on a socket: http://www.php.net/manual/en/function.socket-listen.php
I suggest you read and try them out by simply testing then with a unix tool called netcat (nc <ip_address> <port> command normally)
Excuse my terrible pun.
Basically, the deal is, the host is being a bitch. A simple fsockopen script, no matter what port, server, or anything, will connect. Trying to get server help from the webhost (due to unfortunate circumstances I have no way of changing hosts) proved ineffective... bunch of morons. A simple phpinfo() shows, however, that allow_url_fopen is on and Registered Stream Socket Transports allows me tcp, udp, unix, udg, ssl, sslv3, sslv2, tls.
So, what am I missing? Does it sound like the host is discreetly blocking socket connections? I mean, even port 80 doesn't work:
Warning: fsockopen() [function.fsockopen]: unable to connect to www.google.com:80 (Connection timed out)
Provided you are calling fsockopen correctly (you don't show code), that error is implying that yes, they are restricting you from making outbound connections.
$fp = fsockopen("www.google.com", 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
}
If you have shell access, try using telnet and you should see the same result:
telnet www.google.com 80
I'm using this to check for the availability of a URL:
$fp = fsockopen($url, 443, $errno, $errstr);
and I get this error back...
Warning: fsockopen() [function.fsockopen]: unable to connect to https://example.com/soapserver.php:443 (Unable to find the socket transport "https" - did you forget to enable it when you configured PHP?) in C:\Home etc etc....
I'm using an IIS server btw,( no its not my doing! ) so I think its something to do with not having open-ssl, but I'm not sure. Can anyone help please?
I did a phpinfo() and I do have ssl, but on IMAP and cURL, thats all.
Any ideas?
Uncomment the line: extension=php_openssl.dll in php.ini
You should be using just the hostname, not the URL in the fsockopen call. You'll need to provide the uri, minus the host/port in the actual HTTP headers. As #Martijin noted, and as listed in the manual page, you'll need to preface your host name with ssl:// for SSL or tls:// if using transport layer security.
Manual page for fsockopen. Look at Example #1.
also for ssl you need to prefix the host with ssl://
Let's say you wanted to grab NY Times, which enforces HTTPS:
Incorrect:
$client = stream_socket_client('https://www.nytimes.com', $errno, $errstr, 30);
Correct:
$client = stream_socket_client('tcp://www.nytimes.com:443', $errno, $errstr, 30);
Note I've replaced https:// with tcp:// and appended the 443 port to the hostname.
I guess we can say that stream_socket_client() does not speak URLs.
Switching to ssl:// worked for me but I kept getting a BAD REQUEST response. I found that I needed to add one more line to declare explicitly my Host Header as described here and ensure that I've updated my HTTP from HTTP/1.0 to HTTP/1.1:
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
Check curl installed or not for php.
if it is not installed install the curl.
for windows Uncomment the line: extension=php_openssl.dll in php.ini,
for ubuntu sudo apt-get install php-curl