Hallo,

my site is PHP MySQL based. The links call URLS from the database (index.php?cat=home&id=home) and the index.php page loads a text/php file from a directory, depending on the database entry. For example the above example will have an include(content/home/home.php) record in the database.

What I want to do now is to make sure that if someone calls the test/php file directly (e.g. http://www.mysite.com/content/home/home.php then a php script will recognise this and redirect the user to http://www.mysite.com/index.php?cat=home&id=home which will show the same text/php file but in the index template.

Because the index.php page is php and because the individual content pages have to be in php (otherwise when someone clicks directly on one of these, nothing will happen - no redirect), I could not use header (Locate) because then I got the error that the headers had already been sent by index.php - which is true. Thus I used javascript - here is the code:

<?php
$myurl = "$HTTP_HOST$PHP_SELF";
$insertGoTo = 'http://www.softwarecontracts.de/index.php?cat=about&id=home';
echo "$myurl";
if ($myurl != 'http://www.softwarecontracts.de/index.php') 
{
print "<script language=\"JavaScript\">";
print "window.location = '$insertGoTo' ";
print "</script>";
} 
?>

However, the script keeps reloading and redirecting! I think this is because the ouput of $HTTP_HOST$PHP_SELF only prints out until the .php - i.e. http://www.mysite.com/index.php and not http://www.mysite.com/index.php?cat=home&id=home . I though then, it doesn't matter about this - the page content/home/home.php should never be called alone anyway - thus, if the page is NOT http://www.mysite.com/index.php (which it wouldn't be if a user called the /content/home/home.php page directly), then a redirect would do the job nicely. I was wrong - I want it to either work the way I intended, ot just redirect once. Is there anyway of doing this in PHP - or any way of printing the entire URL (i.e. with index.php?cat=home&id=home)? Would appreciate any help with this.

    I think this somewhat agricultural solution has solved the problem:

    <?php
    $myurl1 = "print \"<script language=\"JavaScript\">";
    $myurl2 = "print \"document.writeln(document.location)\"";
    $myurl1 = "print \"</script>\"";
    $myurl = "$myurl1$myurl2$myurl3";
    
    print "</script>";
    $insertGoTo = 'http://www.softwarecontracts.de/index.php?cat=about&id=home';
    echo "$myurl";
    if ($myurl != $insertGoTo)
    {
    print "<script language=\"JavaScript\">";
    print "window.location = '$insertGoTo' ";
    print "</script>";
    } 
    ?>

    If any further problems crop up I'll post them

      I do this that way:
      - I declare in index.php some global varible

      $whatever="is_template";

      - then in other, included files I check if this varible is declared or not (if not - redirect to index.php?andsoon)

      <?php 
      if (isset($whatever) && $whatever=="is_tempate")
      {
      //whatever code you need
      }
      else
      header("Location: index.php?and_so_on");
      ?>

      if it's not declared, it means that user has come to the page directly, and therefore none input had been sent - sending a header is possible. If it is set, that means everything is all right, no need to try to send any headers.

        Write a Reply...