Training, Open Source computer languages
PerlPHPPythonMySQLApache / TomcatTclRubyJavaC and C++LinuxCSS 
Search for:
Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
Python, Lua and Tcl - public course schedule [here]
Private courses on your site - see [here]
Please ask about maintenance training for Perl, PHP, Java, C, C++, Ruby, MySQL and Linux / Tomcat systems
Redirect to another page

Posted by Chris_Isaac (Chris Isaac), 11 February 2004

The code should print out a list of towns(hyperlinked to files of the same name) then redirect the user to that page if it exists, or print a warning message if it doesn't.

The following code works fine except for the redirect bit, I've set the header info at the top, with a if statement to trigger it off.  It does print the messages, but then doesn't redirect.

Can you see anything obvious I've done wrong?

/////////////////// redirects the page if town selected exists  //////////////////////


////////////////  Sets include file and access database  /////////////////////
include ("c:/Program Files/Apache Group/Apache2/htdocs/");
topper("Chris's Local Machine");

$connect=mysql_connect("localhost","root","") or die("Cannot connect to server");
$database=mysql_select_db("accoms") or die("Cannot connect to database");
$query="select town from locations";

while($row=mysql_fetch_array($rs)) {


///////////////////   Prints a complete list of Towns (with hyperlinks to follow)  ///////////
for ($i=0; $i<=$count; $i++){
     print "<a href=\"http://localhost/towns.php4?nexty=2&town=$townarray[$i]\">$townarray[$i]</a><br>";


//////////  Should print the 1st line, then redirect if file exists, else print warning message  //////////
if ($nexty==2){
     print"it choose $town<br>";

     if (file_exists("c:/Program Files/Apache Group/Apache2/htdocs/$town.php4")){
           print"should redirect";
                       print"No $town does not exist";


Posted by admin (Graham Ellis), 11 February 2004
I'm confused.

"Redirect" goes in the header, and can only go to one page .... you are showing a series of links that will be followed when you select a link.

Do you want the page to be automatically replaced by another after a certain number of seconds, or so you want your users to click on a piece of text to jump to another page of their choice?

You can't have it offering a big list of pages then move on to one automatically without any user input, as it won't know which one to move on to!

P.S.  With "localhost" and "C:" paths all over the place, your code is pretty unportable .... suggest you leave the host name out of  the URL and you use relative paths!

Posted by Chris_Isaac (Chris Isaac), 11 February 2004
Your confused...  I'm confused and I wrote it..  

Sorry, the header to redirect should be a varible $town (generated by the town hyperlinks below), not a fixed place as above.

Basically what I would like to happen is a list of towns is generated, eg:

etc, etc

Each of the above is a hyperlink, if pressed, will go to the town page of that name, eg, newport.php4 if that page exists, or print an error message if it doesn't (actually I want it to generate the page automatically, but will work this bit out next).

Hope this clears things a bit.

Also note taken about the portability, will address this to.


Posted by admin (Graham Ellis), 12 February 2004
I'm guessing that the links are appearing but don't follow properly? I'm almost certain it's failing because of your link to "localhost" which means that your web visitor will be directed to a script on HIS OWN COMPUTER when he clicks the link ...

Suggestion for testing.  Generate the page with the problem links and use your browser's view source to examine what you've actually generated.   Try typing the URLs shown in the source into your location box and see what's wrong with them that way - a sort of debug mode.  You can play with the URL in the location box until you get the page you want, then transfer the changes you had to make back to your script.

Posted by Chris_Isaac (Chris Isaac), 12 February 2004
The hyper links are working, if for example you click on newport, it does print out the correct message depending if Newport exists or not, if it exists, it prints out "yes blah blah Newport exists" then "should redirect" as in the following bit of code:  

if (file_exists("c:/Program Files/Apache Group/Apache2/htdocs/$town.php4")){
 print"should redirect";

but then doesn't go anywhere.  Could the problem be in $redisrect=2;  or perhaps the following :


I'll look at the source as well later to see if I can see anything there.


Posted by Chris_Isaac (Chris Isaac), 13 February 2004
As Dora the Explorer would say "I did it!!"

I had my test statement in the wrong place, I needed it at the top just before the header statement.

Thanks, chris.

This page is a thread posted to the opentalk forum at and archived here for reference. To jump to the archive index please follow this link.

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2018: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01225 708225 • FAX: 01225 793803 • EMAIL: • WEB: • SKYPE: wellho