Archive for the ‘Programming – Computers’ Category

How to block keyboard keys in forms with javascript

A client asked me to block the ENTER key in a form I created for him. After some research, here is the piece of code you have to enter into the page, which will block the event:

<script type=”text/javascript”>
$(document).ready(function() {
document.onkeypress = stopRKey;
});
function stopRKey(evt) {
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type==”text”))  {return false;}
}
</script>
The 13 in the code above is the code for the ENTER key, replace that if you want to block an other keypress.

How to secure your Apache web server

I have wamp installed on my local PC with static IP. As I use it for development, sometimes I am sending the customers links to applications, to check and feedback them.

A problem I experienced is that Google indexed 2 files on my PC. How he did that remains a mistery for me, but for sure this is a HUGE security problem, databases and important mails got public. So, how did I solve the problem? I created a .htaccess file like this:

Allow from xx.xxx.xxx.xxx
Deny from all
AuthUserFile c:\wamp\pwds\.htpasswd
AuthName “Members Only”
AuthType Basic
require valid-user
What the above means:
1. I am now filtering the visitors based on their IP address. Right now only my IP is enabled, the rest will get an Access Forbidden error message.
2. Secondly, for the IPs enabled, there is a username-password combination which needs to be known. They are set in the .htpasswd file, something like:
username:password

RIP IE6 – How to block Internet Explorer 6 from displaying web pages

As I stated in a previous post, Internet Explorer 6 is the nightmare of every web developer. It’s slow, it’s insecure and it’s stupid. Unfortunately, the fact that it’s more than 10 years old is not stopping some users from using it. Resolving the compatibility issues that usually arise sometimes is unworthy, so here is a little tweak to block the users using IE6 (or lower!):

In the <head> of the html file, include:

<!–[if lte IE 6]>
<script type=”text/javascript” src=”./js/rip_ie6.js”></script>
<![endif]–>
The content of the Javascript file is quite simple:

$(window).load(function() {
$(‘body’).css(“background-color”,”#414141″);

var message = ‘Sorry, but your browser (Internet Explorer 6) is not suported by this site. Please update to a newer version, or try an alternative browser:’;

var links = ‘<br><br><a href=”www.mozilla.com” title=”Mozilla Firefox”>Mozilla Firefox</a>’;
links += ‘<br><br><a href=”www.google.com/chrome” title=”Google Chrome”>Google Chrome</a>’;

$(‘body’).html(‘<div style=”width:600px;height:254px;background:white;margin:200px auto;padding: 20px;”><img src=”js/rip_ie6.png” alt=”" style=”float:left;margin-right:20px;”>’+message+links+’</div>’);

});

As you can see, I offer the user the possibility to download Firefox or Chrome, trying to minimize the damage suffered.
Of course, your web-page needs to have jQuery installed for the above code.

.htaccess regular expressions cheat-sheet

It’s always good to have the most used htaccess regular expressions in one place. Here is a little help (cheat-sheet), which contains all the special characters which you can use in your htaccess file to rewrite the urls:

. (full stop) – match any character
* (asterix) – match zero or more of the previous symbol
+ (plus) – match one or more of the previous symbol
? (question) – match zero or one of the previous symbol
\? (backslash-something) – match special characters
^ (caret) – match the start of a string
$ (dollar) - match the end of a string
[set] – match any one of the symbols inside the square braces.
(pattern) – grouping, remember what the pattern matched as a special variable

Get Client IP with Javascript

Ever wondered how to get the client’s IP address from Javascript? I have to set it in a Cookie variable now, so here is what I found, a clean and easy solution:

<script type=”application/javascript”>

function get_my_ip(json){
alert(json.ip);
}
</script>
<script type=”application/javascript” src=”http://jsonip.appspot.com/?callback=get_my_ip”></script>