Category Archives: ASP.Net Code

A PHP version of the ASP.Net GridView control

I've always loved the GridView control in ASP.Net. It made data viewing, sorting, paging and editing so easy and mindless.

Share

Determine a visitor's location by their IP address!

Tracking a visitor's geographical location (ie. country, region, city, latitude, longitude, ZIP code, ISP and domain name) used to be a cool trick. Now it is an integral part of many websites, enabling them to identify the locations from where they're getting the maximum traffic; and tune your webpages accordingly. It also enables you to modify the site according to location, or send users to specific sections (ie. show prices in CAD for Canadian visitors).

It is relatively easy to use this technology on your website with IP2Location's proprietary IP address lookup database. This database is free for the IP-Country version. Of you need more details like region, city, latitude, longitude, ZIP code, ISP and domain name for the IP address, you need to purchase the full database.

Share

Convert an IP address to IP number with PHP, ASP, C# and VB.Net

So you've retrieved the visitor's IP address. If you're just logging or tracing it, that's all you need to do. However, if you want to match it to an IP location database, you can't search by the IP in the IPV4 format. It needs to be converted to its IP number equivalent (it is just more efficient to store and search between a range of numbers in database). Here are the functions in PHP, ASP, C# and VB.Net to convert and IP address from dot format to IP number format.

Each function is based on the fact that IP addresses (IPV4) are divided into 4 sub-blocks. Each sub-block has a different weight number, each powered by 256. Here's the math:

IP Number = (16777216*a) + (65536*b) + (256*c) + (d (1))
– where the IP Address = a.b.c.d

And here are the functions that implement that math:

Share

How to retrieve a visitor's IP address

Every visitor to your site or web application has an IP address. It is quite handy to be able to get that address. It can be used for security logging, or perhaps tracing. It can also be used to determine where they are in the world, or at least where their ISP is.

The difficulty is when they're behind a proxy of some sort, you only see the IP address of the proxy server. So, here are the code snippets in PHP, ASP and .Net that first check for an IP addresses that's forwarded from behind a proxy, and if there's none then just get the IP address.

Share

How to redirect a webpage in PHP, ASP, .Net, IIS, and .htaccess

If you've moved a domain or page, you need to redirect visitors. To transfer search engine links and ranking, you should use a 301 (HTTP code) redirect. The code "301" is interpreted by browsers and search engines as "moved permanently".

Here's how to implement URL Redirection with several web technologies:

PHP


<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>

ASP


<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently" 
Response.AddHeader "Location", " http://www.new-url.com"
>

ASP .NET


<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location","http://www.new-url.com");
}
</script>

IIS

  1. In Internet Services Manager, right click on the file or folder you wish to redirect
  2. Select the radio button titled "a redirection to a URL".
  3. Enter the redirection page URI
  4. Check "The exact url entered above" and the "A permanent redirection for this resource"
  5. Click on 'Apply'

.htaccess – redirect to new domain

Create a .htaccess file in the root directory of your old website with the below code, and all your directories/pages of your old domain will be redirected to your new domain.


Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.yournewdomainurl.com/$1 [R=301,L]

* Apache Mod-Rewrite module must be enabled

.htaccess – redirect to www

Create a .htaccess file in the root directory of your old website with the below code, and all your directories/pages of your old domain will be redirected to your www.yourdomain.com.


Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.yourdomain.com/$1 [r=301,nc]

* Apache Mod-Rewrite module must be enabled

Share