Get URL

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob Bedford

    Get URL

    I use PHP to "include" dinamically some pages. I don't want to use frames.
    Now, my default.php (that acts like a frame) loads a leftmenu.php and a
    rightform.php.

    What I want to do in my rightform.php, is to detect when it's name is in the
    URL (like www.mysite.com/rightform.php) If it's the case, I want to redirect
    to the index.php page of the site. Since the URL is on the client side, I
    think this should be done in javascript. (also the server receives the URL,
    so it may be possible to do server side tests), and once I detect the URL
    names contains the filename that should not be in the URL, redirect to
    index.php.

    How to do so ?

    Bob


  • kaeli

    #2
    Re: Get URL

    In article <4110db4b$0$487 4$5402220f@news .sunrise.ch>, bedford1
    @YouKnowWhatToD oHerehotmail.co m enlightened us with...[color=blue]
    > What I want to do in my rightform.php, is to detect when it's name is in the
    > URL (like www.mysite.com/rightform.php) If it's the case, I want to redirect
    > to the index.php page of the site.[/color]

    Vital functioning of a site is always best done on the server if at all
    possible.
    Use PHP and check the server variable request_uri and redirect accordingly.



    --
    --
    ~kaeli~
    Why do they sterilize the needles for lethal injections?



    Comment

    • Grant Wagner

      #3
      Re: Get URL

      Bob Bedford wrote:
      [color=blue]
      > I use PHP to "include" dinamically some pages. I don't want to use frames.
      > Now, my default.php (that acts like a frame) loads a leftmenu.php and a
      > rightform.php.
      >
      > What I want to do in my rightform.php, is to detect when it's name is in the
      > URL (like www.mysite.com/rightform.php) If it's the case, I want to redirect
      > to the index.php page of the site. Since the URL is on the client side, I
      > think this should be done in javascript. (also the server receives the URL,
      > so it may be possible to do server side tests), and once I detect the URL
      > names contains the filename that should not be in the URL, redirect to
      > index.php.
      >
      > How to do so ?
      >
      > Bob[/color]

      This is best done by the PHP files themselves. Each PHP file can determine the
      _SERVER["REQUEST_UR I"], _SERVER["SCRIPT_NAM E"], _SERVER["PATH_TRANSLATE D"] or
      _SERVER["PHP_SELF"] and take the appropriate action based on the value.

      For example, in rightform.php, you might have:

      <?php
      if (strpos(_SERVER["REQUEST_UR I"], "rightform.php" ) !== 0) {
      header("locatio n:index.php");
      }
      ?>

      Now rightform.php can detect if it was the loaded URI and redirect to index.php
      all by itself. I threw this together pretty quick, it may not work correctly as
      written.

      --
      Grant Wagner <gwagner@agrico reunited.com>
      comp.lang.javas cript FAQ - http://jibbering.com/faq

      Comment

      • Thomas 'PointedEars' Lahn

        #4
        Re: Get URL

        [Full quote because of x-post]

        Grant Wagner wrote:
        [color=blue]
        > Bob Bedford wrote:[color=green]
        >> I use PHP to "include" dinamically some pages. I don't want to use
        >> frames. Now, my default.php (that acts like a frame) loads a leftmenu.php
        >> and a rightform.php.
        >>
        >> What I want to do in my rightform.php, is to detect when it's name is in
        >> the URL (like www.mysite.com/rightform.php) If it's the case, I want to
        >> redirect to the index.php page of the site. Since the URL is on the
        >> client side, I think this should be done in javascript. (also the server
        >> receives the URL, so it may be possible to do server side tests), and
        >> once I detect the URL names contains the filename that should not be in
        >> the URL, redirect to index.php.
        >> [...][/color]
        >
        > This is best done by the PHP files themselves. Each PHP file can determine
        > the _SERVER["REQUEST_UR I"], _SERVER["SCRIPT_NAM E"],
        > _SERVER["PATH_TRANSLATE D"] or _SERVER["PHP_SELF"] and take the appropriate
        > action based on the value.
        >
        > For example, in rightform.php, you might have:
        >
        > <?php
        > if (strpos(_SERVER["REQUEST_UR I"], "rightform.php" ) !== 0) {
        > header("locatio n:index.php");
        > }
        > ?>[/color]

        <OT>

        1. In PHP, variable declarations and references must be
        prefixed with "$".

        2. You want to avoid double-quoted strings where no expansion
        is required.

        3. The $_* superglobals are only available from PHP 4.1 on.

        <?php
        /**
        * Retrieves the value of an element of an associative array.
        *
        * This is designed for $_GET and other HTTP variables but
        * also works for common associative arrays.
        *
        * @author
        * Copyright (C) 2004 Thomas Lahn &lt;php@Pointed Ears.de&gt;
        * @param string
        * Key identifier. The default is the empty string.
        * @param string
        * Array identifier. The default is '_GET'. If there is
        * no such array, the identifier is prefixed with 'HTTP'
        * and suffixed with '_VARS' (to support the deprecated
        * HTTP_GET_VARS etc. arrays as of PHP &lt; 4.1). If
        * there is still no array with that identifier, return
        * the empty string.
        * @return
        * The value of the element of that array with that key or
        * the empty string if there is no such element or array.
        */
        function getVars($key = '', $array = '_GET')
        {
        global ${$array};
        global ${'HTTP'.$array .'_VARS'};

        return (isset(${$array }) && isset(${$array}[$key])
        ? ${$array}[$key]
        : (isset(${'HTTP' .$array.'_VARS' })
        && isset(${'HTTP'. $array.'_VARS'}[$key]))
        ? ${'HTTP'.$array .'_VARS'}[$key]
        : '');
        }

        if (strpos(getVars ('REQUEST_URI', '_SERVER'), 'rightform.php' ) !== 0)
        {
        header('locatio n:index.php');
        }
        ?>

        But I would rather use

        <?php
        ...

        if (preg_match(
        "'/?rightform.php([?#]|$)'",
        getVars('REQUES T_URI', '_SERVER'))
        {
        header('Locatio n: index.php');
        }
        ?>

        or the RedirectMatch directive of the Apache HTTP Server.
        [color=blue]
        > Now rightform.php can detect if it was the loaded URI and redirect to
        > index.php all by itself. I threw this together pretty quick, it may not
        > work correctly as written.[/color]

        Yes, indeed.
        [color=blue]
        > --[/color]

        Please update your UA to use a correct signature delimiter (DashDashSpace) ,
        see <http://insideoe.tomste rdam.com/>.


        X-Post & F'up2 cl.php

        PointedEars

        Comment

        Working...