Training, Open Source computer languages

This is page http://www.wellho.net/forum/Writing-PHP/Redirect ... -page.html

Our email: info@wellho.net • Phone: 01144 1225 708225

 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
Redirect to another page

Posted by Chris_Isaac (Chris Isaac), 11 February 2004
Hi

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?

Code:
<?php
/////////////////// redirects the page if town selected exists  //////////////////////

if($redirect==2){
     header("Location:http://localhost/Aberdare.php4");
     exit();
}

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

if($nexty!=2){
$townarray=array();
$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";
$rs=mysql_query($query,$connect);

while($row=mysql_fetch_array($rs)) {
     array_push($townarray,$row[town]);

}

///////////////////   Prints a complete list of Towns (with hyperlinks to follow)  ///////////
$count=count($townarray);
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  //////////
$nexty=$_GET[nexty];
$town=$_GET[town];
if ($nexty==2){
     print"it worked.........you choose $town<br>";

     if (file_exists("c:/Program Files/Apache Group/Apache2/htdocs/$town.php4")){
           print"should redirect";
                       $redirect=2;
     }
     else{
                       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:

Aberdare
Cardiff
Newport
Melksham
Swindon
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.

Thanks.

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:  

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


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

Code:
if($redirect==2){
header("Location:http://localhost/Aberdare.php4");
exit();
}


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

Thanks

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 www.opentalk.org.uk and archived here for reference. To jump to the archive index please follow this link.

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho