Results 1 to 4 of 4

Thread: Redirect script in php

  1. #1
    Join Date
    Jan 2009
    Posts
    41

    Redirect script in php

    hello i am hoping some help from here ,i am looking for code which will use to redirect url in php. i want this for following change "i have an old file that has SERP but i have structure and i want to leave that file in place but point it to the new file". please give me possible help as soon as thank you very much.

  2. #2
    Join Date
    May 2008
    Posts
    2,389

    Re: Redirect script in php

    for redirection you can use the header() function.You only have to take care that header() must be called before any actual output is deliver
    <?php

    header('Location:http://www.php.net');

    $f = fopen('demo.txt','w+');

    fwrite($f,'Test');

    fclose($f);

    ?>

  3. #3
    Join Date
    Dec 2008
    Posts
    47

    Re: Redirect script in php

    This is PHP redirect() function, hope this will work for you ,use it .

    // func: redirect($to,$code=307)
    // spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
    function redirect($to,$code=301)
    {
    $location = null;
    $sn = $_SERVER['SCRIPT_NAME'];
    $cp = dirname($sn);
    if (substr($to,0,4)=='http') $location = $to; // Absolute URL
    else
    {
    $schema = $_SERVER['SERVER_PORT']=='443'?'https':'http';
    $host = strlen($_SERVER['HTTP_HOST'])?$_SERVER['HTTP_HOST']:$_SERVER['SERVER_NAME'];
    if (substr($to,0,1)=='/') $location = "$schema://$host$to";
    elseif (substr($to,0,1)=='.') // Relative Path
    {
    $location = "$schema://$host/";
    $pu = parse_url($to);
    $cd = dirname($_SERVER['SCRIPT_FILENAME']).'/';
    $np = realpath($cd.$pu['path']);
    $np = str_replace($_SERVER['DOCUMENT_ROOT'],'',$np);
    $location.= $np;
    if ((isset($pu['query'])) && (strlen($pu['query'])>0)) $location.= '?'.$pu['query'];
    }
    }

    $hs = headers_sent();
    if ($hs==false)
    {
    if ($code==301) header("301 Moved Permanently HTTP/1.1"); // Convert to GET
    elseif ($code==302) header("302 Found HTTP/1.1"); // Conform re-POST
    elseif ($code==303) header("303 See Other HTTP/1.1"); // dont cache, always use GET
    elseif ($code==304) header("304 Not Modified HTTP/1.1"); // use cache
    elseif ($code==305) header("305 Use Proxy HTTP/1.1");
    elseif ($code==306) header("306 Not Used HTTP/1.1");
    elseif ($code==307) header("307 Temorary Redirect HTTP/1.1");
    else trigger_error("Unhandled redirect() HTTP Code: $code",E_USER_ERROR);
    header("Location: $location");
    header('Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0');
    }
    elseif (($hs==true) || ($code==302) || ($code==303))
    {
    // todo: draw some javascript to redirect
    $cover_div_style = 'background-color: #ccc; height: 100%; left: 0px; position: absolute; top: 0px; width: 100%;';
    echo "<div style='$cover_div_style'>\n";
    $link_div_style = 'background-color: #fff; border: 2px solid #f00; left: 0px; margin: 5px; padding: 3px; ';
    $link_div_style.= 'position: absolute; text-align: center; top: 0px; width: 95%; z-index: 99;';
    echo "<div style='$link_div_style'>\n";
    echo "<p>Please See: <a href='$to'>".htmlspecialchars($location)."</a></p>\n";
    echo "</div>\n</div>\n";
    }
    exit(0);
    }

  4. #4
    Join Date
    May 2008
    Posts
    2,297

    Re: Redirect script in php

    <? php
    header ( "Location: http://site address.com");
    >
    <html>
    <head>
    <title> Redirect in PHP </ title>
    </ head>
    <body>
    The code should be all 'top of page or return an error.
    Replace http://site address.com with the site you want to be redirected to the visitor.
    </ body>
    </ html>

Similar Threads

  1. Replies: 3
    Last Post: 17-08-2010, 09:06 PM
  2. Is it possible to execute Perl script within another script?
    By RasMus in forum Software Development
    Replies: 2
    Last Post: 21-07-2009, 10:57 PM
  3. Word 2008 + bibfuse: no script in script menu
    By deval4u in forum Software Development
    Replies: 5
    Last Post: 06-04-2009, 12:53 PM
  4. Replies: 2
    Last Post: 14-01-2009, 01:25 PM
  5. Startup Script or Login Script ??
    By WANNABE in forum Active Directory
    Replies: 5
    Last Post: 22-12-2006, 07:44 PM

Tags for this Thread

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
  •  
Page generated in 1,713,571,838.24782 seconds with 16 queries