header

 

More links

AutoHotkey | Android | Arduino | COMM140Fractals | Grammar Checkers | Knots | A Million Dots Activity |  Processing | Processing for Scratch Users | RedBubble | Tutorials | Weather | World Time Meeting Planner | Favicon Generator.

Home > PHP Links & Examples > Examples > Where am I?

I am:
Note:

Note posted: April 11, 2024 at: 08:32 pm

Where am I?

Note: The following PHP code works on UNIX, Linux servers etc. It is not designed for Windows servers.

The "Where am I?" php code comes in 3 parts.
Download whereami.zip for a copy of each of the following 3 php files as well as the 5 text files used.
echo-i-am.php Displays a single cell table with my whereabouts
i-am.php Try this example to see how it all works
update-i-am.php Updates the 5 text files and returns to the webpage using the HTML Tag <meta http-equiv="REFRESH" content="0; URL=index.php">

Once you have placed all three parts onto your webserver you then have to then place a copy of the following 5 text files onto your webserver.

i-am.txt
i-am-date.txt
i-am-note.txt
i-am-time.txt
bg-colour.txt

The next step is to change the permissions of the 5 text files to allow the webserver to write to these text files for you. If you have difficulty with this step you will need to contact your webserver adminsitrator.

Because my webserver runs on a UNIX system and I use WS_FTP LE to publish my webpages I also use WS_FTP LE to change the permissions for me. If WS_FTP can not change the file permissions it may be because your webserver does not support the "SITE chmod" command contact your webserver administrator and ask them to turn it on, or ask for advise on changing file permissions. If you would like to download a copy of WS_FTP LE you can obtain a copy from here it is a 691 KB file free for educational use.

If you do not want to use WS_FTP Lite to change the file permissions then you could use a telnet client such as PuTTY and the UNIX chmod command. eg chmod 666 i-am.txt

See below for details of how to use WS_FTP LE to change the file permissions.

Part 1 is a small PHP file called echo-i-am.php a copy of this file must be sent to your Web server I use WS_FTP to do this.

This file creates a table with a single cell that reports my whereabouts. Where ever I want this to appear in one of my webpages I insert the following line of PHP code into the HTML source code of my webpage.

<?php include("echo-i-am.php"); ?>

See below to see what this does:

I am:
Note:

Note posted: April 11, 2024 at: 08:32 pm

Note: The include command inserts the code from one file into another. This is a good way to create a header or a footer for your webpages. If you do this then you will only need to edit the header in one place and changes will appear on all webpages that contain an include link to the header. This is how I add the menu to the top of all my php pages.

 

The file echo-i-am.php contains the following code:

It should be saved as a seperate .php file and placed in your webpage folder.
A copy of echo-i-am.php should be sent to the web server.

 

<?php
/***********************************************************
* Author Rupert Russell
* e-mail: r.russell at ballarat.edu.au
* filename: echo-i-am.php
* date: September 18, 2004
*
* purpose: To generate a single cell table that
* that report my whereabouts using the following 5 text files:

*   i-am.txt
*   i-am-note.txt
*   i-am-date.txt
*   i-am-time.txt

*   bg-colour.txt
*
* See also: i-am.php
* update-i-am.php
************************************************************/

// used for time offset
putenv( "-3749+14458");

// Reads the iAm files and reports where I am

$iAmFile = ("i-am.txt");
$iAmNoteFile = ("i-am-note.txt");
$bgColourFile = ("bg-colour.txt");
$iAmDateFile = ("i-am-date.txt");
$iAmTimeFile = ("i-am-time.txt");

//now we read the files
$iAm = file($iAmFile);
$iAmNotes = file($iAmNoteFile);
$bgColour = file($bgColourFile);
$iAmDate = file($iAmDateFile);
$iAmTime = file($iAmTimeFile);

//display the text in a single cell table
echo "<table border=\"1\" cellpadding=\"15\" width=\"100%\">";

echo "<tr>";
echo "<td bgcolor=\"$bgColour[0]\">";
echo "I am: <b>$iAm[0]</b><br>";
echo "Note: $iAmNotes[0]<p>";
echo "<em>Note posted: $iAmDate[0] at: $iAmTime[0]</em>";
echo "</td>";
echo "</tr>";
echo "</table>";
?>

 

Part 2 of the application is a file called i-am.php which contains a form that allows for fast selection of common values for my location.

Download a copy of this file and open it in in the webpage editor of your choice. You will want to edit the values of the possible locations.

Screen shot of i-am.php

 

Part 3 of the application is a file called up update-i-am.php this writes the following 5 text files to the webserver:

i-am.txt
i-am-note.txt
bg-colour.txt
i-am-date.txt
i-am-time.txt

Download a copy of this file and place it on the webserver.

It contains the following code:

<?php
/***********************************************************
* Author Rupert Russell
* e-mail: r.russell at ballarat.edu.au
* date: September 18, 2004
* purpose: To write to the 5 text files that store information
* on my whereabouts.
*
* The 5 files are:
* i-am.txt
* i-am-note.txt
* bg-colour.txt
* i-am-date.txt
* i-am-time.txt
*
* See also: i-am.php
* echo-i-am.php
************************************************************/

// Get required data
$filename1 = 'i-am.txt';
$filename2 = 'bg-colour.txt';
$filename3 = 'i-am-note.txt';
$filename4 = 'i-am-date.txt';
$filename5 = 'i-am-time.txt';

// Sets the current date to $iAmDate
$iAmDate = date("F d, Y");

// Sets the current time to $iAmTime
$iAmTime = date("h:i a");


// retreive vairables from POST method
$iAm = $_POST['iAm'];
$bgColour = $_POST['bgColour'];
$iAmNote = $_POST['iAmNote'];


// Write i-am.txt
if (is_writable($filename1)) {

   if (!$handle = fopen($filename1, 'w')) {
      echo "Could not open file ($filename1)";
      exit();
   }

   if (fwrite($handle, $iAm) === FALSE) {
      echo "Could not write to file ($filename1)";
      exit();
   }

   fclose($handle);

} else {
   echo "The file $filename1 is not writable";
}


// Write bg-colour.txt
if (is_writable($filename2)) {

   if (!$handle = fopen($filename2, 'w')) {
      echo "Could not open file ($filename2)";
      exit();
   }

   if (fwrite($handle, $bgColour) === FALSE) {
      echo "Could not write to file ($filename2)";
      exit();
   }

   fclose($handle);

} else {
   echo "The file $filename2 is not writable";
}


// Write i-am-note.txt
if (is_writable($filename3)) {

   if (!$handle = fopen($filename3, 'w')) {
      echo "Could not open file ($filename3)";
      exit();
   }

   if (fwrite($handle, $iAmNote) === FALSE) {
      echo "Could not write to file ($filename3)";
     exit();
   }

   fclose($handle);

} else {
   echo "The file $filename3 is not writable";
}


// Write i-am-date.txt
if (is_writable($filename4)) {

   if (!$handle = fopen($filename4, 'w')) {
      echo "Could not open file ($filename4)";
      exit();
   }

   if (fwrite($handle, $iAmDate) === FALSE) {
      echo "Could not write to file ($filename4)";
      exit();
   }

fclose($handle);

} else {
   echo "The file $filename4 is not writable";
}

 

// Write i-am-time.txt
if
(is_writable($filename5)) {

   if (!$handle = fopen($filename5, 'w')) {
      echo "Could not open file ($filename5)";
      exit();
   }

   if (fwrite($handle, $iAmTime) === FALSE) {
      echo "Could not write to file ($filename5)";
      exit();
   }

   fclose($handle);

} else {
   echo "The file $filename5 is not writable";
}
?>

Changing file permissions using WS_FTP LE

The file permissions have to be changed to allow the web server to update the contents of the 5 text files for you.

i-am.txt
i-am-date.txt
i-am-note.txt
i-am-time.txt
bg-colour.txt

I use Ws_FTP to do this:

1) Select the text file(s) on the right hand side of the screen

2) right click on the file(s) and select chmod (UNIX)

3) check Write for Owner, Group & Other

4) click OK

You should see a message saying CHMOD command successful.


APA citation:
Russell, R. (2020, December 05, 04:33 am). Where am I?.
     Retrieved April 19, 2024, from
     http://velorum.ballarat.edu.au/~rrussell/php/examples/whereami/index.php

Last refreshed: April 19 2024. 02:54.44 am

rupert dot russell at acu dot edu dot au Support Wikipedia

Creative Commons License This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.


579 Visits since November 26, 2004


Spam safe e-mail link created by David Orme