SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    Winnipeg Canada
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    PHP Redirect to same page name

    Any suggestions on how I can make this piece of code re-direct to the page title called, rather than to a specific page. I'd like a universal piece of code I can drop in all the original pages rather than writing specific re-directs for each. (.htaccess is not an option I'm afraid - I must place it in the pages)

    ------------- Start Code ----------
    header("HTTP/1.1 301 Moved Permanently");
    header("Location: http://" . $_SERVER["SERVER_NAME"] . "/ca/pages/index.php");
    exit();
    ------------- End Code ------------------------

    IE: I don't want it to go to "/ca/pages/index.php", I would rather it go to "/ca/pages/the page name that was originally referrenced". Even better if it could also include any URL Parameters provided.

    Any suggestions will be appreciated.

  2. #2
    SitePoint Addict bkennedy's Avatar
    Join Date
    Aug 2004
    Location
    michigan
    Posts
    251
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    two server variables that might help, from the php manual

    $_SERVER['PHP_SELF']:

    The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.

    If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available.

    $_SERVER['argv']:

    Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string.

    Hope that helps

    EDIT:
    See predefined variables here:
    http://us3.php.net/manual/en/reserved.variables.php
    ----------------
    bob.kennedy
    ----------------

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2006
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $_SERVER['PHP_SELF'] can be tainted to contain user input, so try not to use it. Try SCRIPT_NAME instead, or your own version of __FILE__ ala:
    PHP Code:
    <?php

    $url 
    = empty($_SERVER['HTTPS']) ? 'http://' 'https://';
    $url .= $_SERVER['HTTP_HOST'];
    $url .= substr(__FILE__strlen($_SERVER['DOCUMENT_ROOT']));

    ?><form action="<?php echo $url ?>">
    <!-- etc. -->

  4. #4
    SitePoint Addict Skookum's Avatar
    Join Date
    Sep 2006
    Location
    Idaho
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also use $_SERVER['REQUEST_URI'] for the website, I found using this helps to contain URL data, such as $_GET variables in the URL. PHP_Self will not retain those variables.

  5. #5
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    Winnipeg Canada
    Posts
    146
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Skookum - Your option worked perferectly, it was exactly what I was looking for.

    Thanks to everyone for your suggestions and help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •