Getting remote IP with PHP when using nginx as gateway 0

When you set up nginx as gateway, you have configuration something along the lines of this:

upstream incoming-address.com {
ip_hash;
server serving-server1.com:80;
server serving-server2.com:80;
}

server {
listen incoming-address.com:80;
server_name incoming-address.com;

location / {
proxy_pass http://incoming-address.com;
}

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

}

Previously without the gateway in between, whenever you wanted to get IP address of the user accessing your site you could use the pre-defined PHP variable $_SERVER['REMOTE_ADDR']. However, with the gateway in between this would now return the gateway IP.

In the nginx as gateway configuration example above this part in the server section is the part that sets the user’s IP forwarding to the web server:

proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

The answer is: with this configuration above to get the user’s IP accessing your site through gateway instead of $_SERVER['REMOTE_ADDR'] you must use $_SERVER['HTTP_X_REAL_IP'].

Or you can re-assign the $_SERVER['REMOTE_ADDR'] parameter.

Compressing static files on web server 0

Now that the average web page size approaches 1MB and people more often use all the possible JS frameworks (such as jQuery, Prototype, etc.) while needing just 0.000001% of the whole solution, and especially not minifying or compressing the files, the web page loading speeds become essential even though you may have all the necessary bandwidth in the world. With all this, people have started using dynamic file-compressing methods that will compress/decompress the files on-the-fly. However, very often the largest files are static like Javascript frameworks mentioned before, therefore compressing them every time on server-side is waste of resources, especially if it is a high-traffic site. Therefore the simple solution is:

1. Compressing the file beforehand (then renaming it back to original file extension like file.js, not file.js.gz)

2. Configuring particular files to be treated as compressed ones (so that they get decompressed when downloaded in web-browser) Read more »

Solution to Firefox 4 not offering to save sessions by default on quitting 0

I had one major disappointment when I installed Firefox 4 after using all the previous versions of Mozilla  – it did not offer to save sessions and open tabs anymore. I read several solutions online for it and most of them presumed that my Firefox profile and preference data were corrupt and since I had still some other Firefox versions on, I believed it. The suggested solution (which appeared to be incorrect) was completely re-installing Firefox. Which I did.. but, naturally, it did not help.

The correct solution was very simple (found here):

1. Enter “about:config” in your Firefox 4 address bar, hit Enter and accept the warning

2. Find setting called “browser.showQuitWarning” which by default is set to “false” and change it to “true”

That’s it! Now Firefox 4 will ask you if you want to save your session when you are about to quit.

P.S. Use at your own risk – if you have this issue, causes of it may still be different than in my case.

Quick way to grab and use server load average data 0

One day I needed to grab just the 3 server load average numbers (more on server load averages here) to log them in a database along with few other parameters for some quick analyses. I queried Google for it, but did not find anything that would work for me in first 5 minutes, so I decided to write a quick load number grabber of my own in PHP and using scheduling tasks with cron.

Read more »

Simply about git (with a step-by-step git workflow example) 2

At first I had hard time understanding how git or any other code versioning systems worked. I tried many of them (SVN, CVS, Mercurial, etc.), I set up the systems, read the necessary documentation, but they still did not make sense to me to use. So I kept versioning the code my own way with folders & backups, etc., which was really clumsy and occasionally caused an error if I forgot to add some changed file. But then, during one project a friend of mine encouraged me to look at git again and we agreed that I can ask him the stupid beginner’s questions I may have. And so I did.. and now, after getting answers to few questions and some terms explained to me, I cannot imagine my code-related work part without using git.

Here I want to explain how I see the git structure and “mindset” in really simple terms identifying things that made me struggle with understanding it. Hopefully this will help people like me.

I will skip the git setup part and creating the repository, since there is good enough documentation for that already. In short, I will write down questions and answers about git I would have liked to know before I started looking at it that I could not find simply explained in any of the documentation I looked at. To sum up – here is all you need to know to start working with git.

Read more »

Embedding YouTube videos in dokuWiki 2

I am using dokuWiki – which is a great and simple open-source tool for maintaining documentation – for several of my projects and today I had a need to add YouTube video to a dokuWiki page. I could not find a way to do that in Google or at first sight also in dokuWiki documentation, but Andreas (the creator of dokuWiki) helped me out in comments to this post.

As per Andreas’ comment, dokuWiki in fact does have a built-in feature for embedding Youtube videos as well as other Flash objects and it is plain simple – add following code when editing a dokuWiki page:

{{http://www.youtube.com/v/RpQvPJZ1h70?.swf?400×333}}

Just replace the red part with your Youtube video code and there you go. Just make sure your dokuWiki version is up to date. As Andreas also explained, opening up the HTML code using the example below allows XSS vulnerabilities, therefore it is not recommended.

Read more »

Internet Explorer 8 on Windows 7 more restrictive (P3P settings) 9

Dealing with handling sessions on 3rd party content (I mean content pulled in using iFrames or Ajax from a different domain than the main page), I recently encountered an issue particularly with Internet Explorer 8 on Windows 7, but not on Windows XP or Windows Vista – our (3rd party) cookies handling sessions were suddenly being blocked. Soon after I found out that the same issue exists for Internet Explorer 6 on Windows XP. I tried to find any issues or topics online in particular what had changed in IE8 on Windows 7, but – nothing. The solution, however, I had to seek for more than a day as I found it using trial&error as it was not described anywhere, but it proved to be pretty simple.
Read more »

Importing/exporting data with MySQL 0

This is more of a note-to-myself entry to dig it up later easily, but the syntax to import dump file to MySQL is:

mysql -p -h database_server -u mysql_user mysql_dbname < file_dbname.sql

The same way mysql dump can be created:

mysqldump -p -h database_server -u mysql_user mysql_dbname > dump_file.sql

Good resource with more on this is here.

Get query string variables of Javascript files 1

Looking at different scripts linking to Javascript and AJAX frameworks such as Scriptaculous, I noticed that it allows loading of particular parts of it by adding them in the following query string such as this:

<script type="text/javascript" src="scriptaculous.js?load=effects,builder"></script>

Looking at scriptaculous.js itself, it relied on some built-in functionality for that. I was unsuccessful in finding online a ready function to do exactly that, so inspired by this piece of code and borrowing the regex part of it, I wrote a function which grabs the additional parameters and allows using them in the Javascript:

function getJSvars(script_name, var_name, if_empty) {

var script_elements = document.getElementsByTagName(‘script’);

if(if_empty == null) {
var if_empty = ;
}

for (a = 0; a < script_elements.length; a++) {
   var source_string = script_elements[a].src;
       if(source_string.indexOf(script_name)>=0) {

       var_name = var_name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
       var regex_string = new RegExp("[\\?&]"+var_name+"=([^&#]*)");
       var parsed_vars = regex_string.exec(source_string);
       if(parsed_vars == null) { return if_empty; }
       else { return parsed_vars[1]; }

      }
   }
}

Usage of it is simple: if you have a Javascript which should parse some dynamic variables (such as user or account ID), you can include it right after the included Javascript like this:

<script type="text/javascript" src="script.js?var1=value1&var2=value2"></script>

instead of creating a server-side generated dynamic Javascript using PHP or other language. The values of variables like var1 can be retrieved like this:

var var1 = getJSvars(‘script.js’, ‘var1′);

You can also add a value as third parameter to output in case var1 is empty, but it is optional.

Now this is scary 0

Could you possibly imagine it is this easy to capture what is typed on your keyboard? Wirelessly.. even through a wall!

Read more »

Next Page »