intercepting mysql traffic in php - php

I don't suppose there's a way to, using PHP, intercept what mysql_query sends out and rewrite it before sending it out?
mysqli::__construct has an option parameter $socket. If streams could be passed for that parameter then you could pass php://memory to it it seems but it doesn't seem to work that way.
Any ideas?

You can setup a proxy server with something like squid that will forward to a database server from the frontend.

Related

PHP - cross-application communication

I have two PHP applications on my server. One of them has RESTAPI which I would like to consume and render in the second application. What is better way then curling the API? Can I somehow ask php-fpm for the data directly or something like that?
Doing curl and making request through the webserver seems wrong.
All this happens on single server - I know it probably doesn't scale well but its small project.
why use REST if you can access the functions directly?
If everything is on the same server then there is no need for some REST, since it makes a somewhat pointless run through the webserver.
But if it is already there and you don't care about the overhead (if there's not much traffic going on then it would make sense), then use file_get_contents instead of curl, it is easier to use, but I doubt it is faster/slower; both are right.
You could also use a second webserver (a second virtualhost) on a different port for internal use. That way things are nicely separated.
(If everything is on different servers, but a local network, then using sockets would be fastest. )
Doing curl and making request through the webserver seems wrong. - I disagree with that. You can still achieve what you want to achieve using Php CURL, even if it's on the same server.
I was in the same problem, but i solved it using MySQL to "queue" tasks, and a worker could use any pooling method, or PHP executing a new server side worker.
Since the results were stored in the same database, the PHP pages could load the results, or the status anytime.

PHP fopen not working on one particular domain

I'm trying to download file from remote url with fopen function.
Problem it's function return false from one website that i need. From other domains functions works fine.
How could it be? Maybe have some options in php? Or that website can protect file from some access(but from browser file available)?
There are a number of checks the server side can do to prevent "miss usage" of their service. One example is a check of the "HTTP Referer Header" which indicates that your request is done by a browser navigating from a link to the object.
You can simulate all that if you want to, but for that you have to find out exactly what the difference is between your request and one the browser successfully makes. Two things to do for that:
find out the exact error message you receive back. Easiest for that is to use php's cURL extension instead of file_open() for your request, it allows you to dump everything you get back. There might be valuable information like a reason in the reply.
monitor both requests by means of a network sniffer, for example tcpdump or wireshark. The comparison of both requests allows to tell the exact difference. That again is the information you need to precisely rebuilt the browsers request in your script.
On some shared hosting or some VPS fopen not work or are disabled inside PHP. Try to use CURL to get contnt. If that not work, the last solution (only if you send some informations via GET but not to recive data) is to use <img> tag and inside "src" to send request. That work ONLY if you send informations, but if you need to recive something, you need to use or AJAX or cURL.

Passing variables to another webserver

Is it possible to pass variables from one webserver to another using php? I need to be able to pass variables from a webhost to a local server for processing and I dont know if it can be done.
You have to communicate between the servers somehow. How you do that communication determines if, and how, you would pass data. (You can't generally pass actual variables, just the contents of them).
If, for instance, you were communicating with HTTP then you could pass the data in the query string of the URI, or in the body of a POST request.
Post a web request to that server.
You can do that using curl and add header data or post data too.
Check php curl manual
Simple post request will also do the trick.
There are many ways you could do this. The simplest way and most service oriented would be to make a "catcher" php script which will recieve the variables on the target server and then the local server can run:
file_get_contents('http://targetserver.com/catcher.php?var1=val1&var2=val2&var3=val3...');
also look into sockets and direct connections, FTP, and other ways to do it.

How to use jQuery AJAX for an outside domain?

I am trying to perform a simple jQuery AJAX attempt using either a .get() or a .post().
If I have a local copy on my server and do:
$.get('/hash.php',...,...)
I monitor in my console in firebug that the get is done and I get a response.
All I change is the URI to an outside server and nothing happens.
$.get('https://secure.mysite.com/subdir/hash.php',...,...)
Doesn't help if I take the 's' off or if I use post instead. Am I missing some parameter that I should use in jQuery?
EDIT: I forgot to mention the reason I'm doing this is because I am eventually migrating from a PHP4 site to a PHP5 site, but for now the live PHP4 site needs a function that isn't in PHP4. So I am calling a PHP5 server to do it. I think I have a good workaround. Thanks!
You cannot send an Ajax Request to another domain than the other on which your application is deployed. This is because of the Same Origin Policy implemented in web-browers -- a security measure.
There are two possible solutions, though :
sending the request to your own server, that will act as a proxy to another (either via a PHP script, or, better, using some of Apache's mod_proxy_http module)
or not using "Ajax", but other techniques, like dynamically creating <script> tags -- which are not subject to the SOP constraint.
It's true that you normally can't do Ajax outside your domain due to the browsers. However using JSONP it is possible to do this. jQuery also has a jsonp param for Ajax now. To make this work you need to control the output of the server though.
Javascript cannot access a server outside of where the javascript file came from.
That is a security feature.
Depending on how browser-specific you want to get you may get around this, but that becomes a bit of a slippery slope.
You cannot do cross domain ajax requests directly, this would be a security concern.
You will need to call your local php file from jquery and have the php file talk to the other domain.
There's a method called JSONP which is used to circumvent that. See the 2nd reply on SO #570100

How do I get using php?

I know that this is a simple question for PHP guys but I don't know the language and just need to do a simple "get" from another web page when my page is hit. i.e. signal the other page that this page has been hit.
EDIT: curl is not available to me.
If curl wrappers are on (they are per default), you can use:
file_get_contents('http://www.example.org');
Note that this happens synchronous, so before the request has completed, your page won't either. It would be better to log access to a logfile (or database) and export the data occasionally. Alternatively, you could do the request after your page has completed, and output has been sent to the client.
Beware file_get_contents() and fopen():
If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.
There's numerous ways... the simplest is file_get_contents('http://...');
Other functions like fopen() also support http: streams.
If you need more control, I'd suggest looking at curl (see curl_init)
There are a number of ways to send a GET request with PHP. As mentioned above, you can use file_get_contents, fopen or cURL.
You can also use the HTTP extension, the fsockopen() function or streams via fopen.
I'd advise checking out what WordPress has done, as it handles almost all possibilities.
You'll probably want to use cURL

Categories