Monthly Archive for April, 2012

How to use the query string in PHP

This article tells you how to pass variables to a PHP page using the query string, and how to access them from that page.

Introduction

Have you ever seen a URL which looked like "www.example.com/page.php?mode=1&style=red"? Well, this page is being passed variables and their values through the query string, here the variables "mode" and "style" are being passed, with values "1" and "red" respectively. The question mark indicates the start of the query string and the ampersand, &, symbol seperates variable=value assignments. The page is then able to read these variables and react according to them, for example to display them to the user.

Passing a query string to a page

There are two ways to pass a QueryString to a page, the first is to enter it manually, for example:

<a href="page.php?mode=1">Mode 1</a>

The above HTML creates a link to a page passing the variable mode with the value "1".
Continue reading 'How to use the query string in PHP'

How to Find the Current URL with PHP

The full URL to a page comes in three parts: The domain name, the path to the file then the filename, and the query string. For example, take the URL http://www.example.com/example/page.php?name=Bob. The three parts of this are:

1. The domain name: www.example.com
2. The path to the page: /example/page.php
3. The query string: name=Bob

So how do you find it all out with your own PHP scripts?

Continue reading 'How to Find the Current URL with PHP'

Find a visitor's IP Address with PHP

Do you want to know the IP address of a visitor? This can be useful for many reasons, such as tracking site usage or blocking access to specific people. Here's how you can find it using PHP.

In a PHP page, within the PHP tags, <?php ... ?>, you can retrieve the IP address of a user through the server variables array, which is contains information about the user and the server environment and is accessible from anywhere in your PHP script. To do this, you use the code $_SERVER['REMOTE_ADDR']. It works like a normal array, where REMOTE_ADDR is the key, and the IP address of the visitor is the value associated with that key.

Display The IP Address

So, if you want to display the IP Address to the user then the following page will suffice:

<?php
echo "Hello! Your IP Address is: " . $_SERVER['REMOTE_ADDR'];
?>

Continue reading 'Find a visitor's IP Address with PHP'

Write Text on to an existing PNG image using PHP

This article will tell you how to write text on an existing PNG image using PHP using PHP's image manipulation functions. The image functions require the GD library to be installed. This article is fairly basic, however some more detail on the basic functions used is included in the article, "write text on a dynamically generated image in PHP"

Continue reading 'Write Text on to an existing PNG image using PHP'

Rotating Images with PHP

This tutorial will show you how to rotate an image in your PHP scripts. You'll need to have the GD library installed to be able to use some of the functions we use in this guide, though this is included with most PHP installations so there's a chance you'll have it.

Image rotation in PHP is handled by a function called imagerotate(), which takes three parameters (and optionally a fourth, which deals with transparency and is beyond the scope of this guide). The first is the resource identifier which you obtain when you load the original image within your script, the second is the angle which you want to rotate the image and the third parameter is used when the image isn't going to be rectangular, and specifies the colour with which to fill the uncovered areas of the rotated image to make it into a rectangle (since all image formats require rectangular images).

Continue reading 'Rotating Images with PHP'

How to use the QueryString in ASP

This article tells you how to pass variables to an ASP page using the QueryString, and how to access them from that page.

Have you ever seen a URL which looked like "www.example.com/page.asp?mode=1&style=red"? Well, this page is being passed variables and their values through the QueryString, here the variables "mode" and "style" are being passed, with values "1" and "red" respectively. The question mark indicates the start of the query string and the ampersand, &, symbol seperates variable=value assignments.

The page is then able to read these variables and react according to them, for example to display them to the user.

Passing a QueryString to a page

There are two ways to pass a QueryString to a page, the first is to enter it manually, for example:

<a href="page.asp?mode=1">Mode 1</a>

The above HTML creates a link to a page passing the variable mode with the value "1".

An alternative, and perhaps more useful, way is to use HTML forms. The main thing to remember with forms is that you need to use the GET method to send information via the QueryString, for example:

<form method="GET" action="page.asp">
Please enter your name: <input type="text" name="username" />
<input type="submit" value="submit" />
</form>

This code displays a simple HTML form with a text box and a submit button. When the user enters their name and presses submit, the information in the name box is passed to a page called "page.asp" (specified in the form's action attribute) via the QueryString. So, if I entered "tiposaurus" and pressed submit, the form will call page.asp?username=tiposaurus. Remember that "username" was what we entered as the text input's name attribute.

Accessing a QueryString element in ASP

In ASP, all the information passed via the QueryString is held in the Request.QueryString collection. To access an item, type Request.QueryString("varName"), where varName is the name of the variable in the QueryString.

To demonstrate, let's create page.asp to process the information from the form above:

<%@language="VBScript"%>
<% If Request.QueryString("username") "" Then
Response.Write "Hello, " & Request.QueryString("username")
Else
Response.Write "You did not enter a name."
End If
%>

So now, me typing in tiposaurus and pressing submit will generate the reply "Hello, tiposaurus".

Listing the contents of Request.QueryString

Should you wish to find out everything that's in the QueryString, you can do so with the following code which displays the variables next to their values in a table:

<%@language="VBScript"%>
<table border="1">
<tr><th>variable</th> <th>value</th></tr>
<%
Dim variable
For Each variable in Request.QueryString
Response.Write "<tr><td>" & variable & "</td>"
Response.Write "<td>" & Request.QueryString(variable) & "</td></tr>"
Next
%>
</table>

Conclusion

This article showed you how to pass information between pages with the QueryString, and a use which allows you to add some increased interactivity. There are a lot of other uses, for example selecting which section of a site to display based on a variable in the QueryString, and processing form data.

How to Find the Current URL in ASP

The full URL to a page comes in three parts: The domain name, the path to the file then the filename, and the QueryString. For example, take the URL http://www.example.com/example/page.asp?name=Bob. The three parts of this are:

1. The domain name: www.example.com
2. The path to the page: /example/page.asp
3. The QueryString: name=Bob

So how do you find it all out with your own scripts?

Well the following code should do it:

<%@language="VBScript"%>
<%
  Dim strDomain, strPath, strQueryString, strURL
  ‘ find out the domain:
  strDomain = Request.ServerVariables("HTTP_HOST")
  ‘ find out the path to the current file:
  strPath = Request.ServerVariables("URL")
  ‘ find out the QueryString:
  strQueryString = Request.ServerVariables("QUERY_STRING")
  ‘ put it all together:
  strURL = "http://" & strDomain & strPath & "?" & strQueryString
  Response.Write "The current URL is: " & strURL
%>

Find a visitor's IP Address in ASP

Do you want to know the IP address of a visitor? This can be useful for many reasons, such as tracking site usage or blocking access to specific people. Here's how you can find it.

In an ASP page, within the ASP tags, <% ... %>, you can retrieve the IP address of a user through the ServerVariables collection of the Request object. To do this, you use the code Request.ServerVariables("REMOTE_ADDR").

Continue reading 'Find a visitor's IP Address in ASP'

Write text on a dynamically generated image using PHP

This tutorial will tell you how to write text to a blank dynamically generated PNG image in PHP. This is fairly simple, so it will just be the first step in a series of tutorials dealing with images in PHP. The image functions require the GD library to be installed.

Continue reading 'Write text on a dynamically generated image using PHP'

How to find the SHA1 hash of a string in Ruby

To find the SHA1 hash of a string, first include the library 'digest/sha1' then use the code below, where 'password' is the string we're computing the hash of.

require ‘digest/sha1′
# ... then later on ...
password = "blah"
sha1_pass = Digest::SHA1.hexdigest(password)