#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0

    Redirecting function


    I tried using the following code to create a redirect function. It works for absolute URLs, but failed for relative URL based on Joomla's root folder. It tries to redirect me to a wrong path f:wampwwwWebsiteName/index.php (the slashes in paths are gone) giving me a 404 error.

    May I know how I can fix the code so that it won't remove the the slashes in JPATH_ROOT so that a user can input a relative path target?
    Below is the code. Thank you.

    <?php

    redirect(JPATH_ROOT. '/'."filename");

    function redirect($url)
    {
    echo '<script type="text/javascript">';
    echo 'window.location.href="' . $url . '";';
    echo '</script>';
    echo '<noscript>';
    echo '<meta http-equiv="refresh" content="0;url=' . $url . '" />';
    echo '</noscript>';
    exit;
    }

    ?>
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    that's not how to do a redirect. You need to issue a Location header with the header() function.

    You also confuse filesystem paths and URLs. You cannot redirect people to a local path on your server. If anybody could browse your whole server, that would be quite insane, wouldn't it? You need a URL. I think JURI::base() is what you're looking for.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    2
    Rep Power
    0
    JURI::base() works.
    Thanks.
  6. #4
  7. No Profile Picture
    Permanently Banned
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0
    header(Locationagename.php);
    for redirecting the page use above code.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    9
    After using a header() redirect, make sure to use the die() function.

    PHP Code:
    header("Location: pagename.php");
    die(); 
    Regards,

    NM.

IMN logo majestic logo threadwatch logo seochat tools logo