Home arrow PHP arrow PHP Networking

PHP Networking

PHP has a great many tools for interacting with a network and also with the Internet. In this article we will look at some of those tools and functions to see how we can use them to make our scripts more useful in a network environment. This article is the first of two parts.

TABLE OF CONTENTS:
  1. PHP Networking
  2. DNS and PHP
  3. DNS and PHP continued
  4. Getting information about a domain name
By: David Web
Rating: starstarstarstarstar / 4
September 08, 2008

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Accessing other websites

Accessing other sites with PHP is extremely easy. But why would you want to access another website? If you just want to get information from a website, for instance a website that has a weather report on a city that is of interest to you, then you can write a spider and get that information and more. I said accessing websites with PHP is easy because you access a website in pretty much the same way that you would access a text file on your hard drive, i.e. by using fopen().


fopen(http://localhost/websecure/ftp.php, “r”);


The fopen() function that we use to open files is also used to open web pages, because a web page is essentially a file on a server. fopen() has the following modes:



Mode

Meaning

r

Reading only, begins reading at the start of the file.

r+

Reading or writing, begins reading at the start of the file.

w

Writing only. Creates the file if it does not exist, and overwrites any existing content.

w+

Reading or writing. Creates the file if it does not exist, and overwrites any existing content (when writing).

a

Writing only. Creates the file if it does not exist, and appends the new data to the end of the file.

a+

Reading or writing. Creates the file if it does not exist, and overwrites any existing contents (when writing).

x

Writing only. Creates the file if it does not exist, but does nothing, issues a warning, if the file does exist.

x+

Reading or Writing. Creates the file if it does not exist, but do nothing, issues a warning, if the file does exist.

You will only be able to open a file for reading, unless the permissions are set otherwise. Also you will have to use a trailing slash after a directory because fopen does not support redirects. For example:


fopen(“http://localhost/websecure/ftp.php/”, “r”);


the above example is fine, but the one below will fail:


fopen(http://localhost/websecure/ftp.php, “r”);


Once you’ve opened a file, you can pretty much treat it the same way you would any other file, using common functions such as file() or fgets() to place or retrieve the data.

An example in code might be something like this:


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

<title>Untitled Document</title>

</head>


<body>

<form action="tester.php" method="get">

<input name="url" type="text" />

<input name="" type="button" />

</form>

</body>

</html>


And the processing code might look something like this:


<?

$url=$_POST['url'];

$fp=file($url);

$n=count($fp);

$r=rand(0,($n-1));

echo trim($fp[$r]);

echo $n;

?>




 
 
>>> More PHP Articles          >>> More By David Web
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 6 - Follow our Sitemap

Dev Shed Tutorial Topics: