hi, i am interested in php link redirect. lets say i have a website with a lot of links, and most of the time if i change one file name i have to update that on everypage, it's irritating,

as for other website like microsoft, they use links like go.microsoft.com/fwlink/id3242 or something like that. most of microsofts desktop app also contains this link. so even if microsoft move their website, or rename a file they redirect to that page. so that means they have a script that contains all the link and also contains where it should redirect. even the changes a file name or move a website, they just have to update that particular link in the script.

so how do i do that!!?? i have no clue, i googled but it shows me php header function or meta refresh which i already know.

The header() function is exactly what you need here. To redirect, simply use it like:

header("Location: xyz.php");

To redirect to the example page xyz.php. If you have a fair amount of pages, then you could store their filenames in an array in a file called go.php:

$pages = array(
    'index.php',   // ID 0
    'contact.php', // ID 1
    'about.php'    // ID 2
);

And simply accept an ID through as a GET value, say $_GET['id'], so to pass this ID to the page, it'd just be a simple matter of redirecting the user to whichever page has the ID which matches the ID specified. So go.php might become:

$pages = array(
    'index.php',   // ID 0
    'contact.php', // ID 1
    'about.php'    // ID 2
);
// If no ID specified, default to ID 0
$id = isset($_GET['id']))? $_GET['id'] : 0;

// If the ID is an existing index of $pages, redirect. Else, display error message
if ($id >= 0 AND $id < sizeof($pages))
{
    header("Location: ".$id);
} else {
    echo("Page not found!")
}

Then, for example, you'd simply have to link to go.php?id=0 to go to the homepage. You could of course expand this to use an associative array (dictionary) to give each page a purposeful ID instead of a number. For example:

$pages = array(
    'home'    => 'index.php',   // ID 'home'
    'contact' => 'contact.php', // ID 'contact'
    'about'   => 'about.php'    // ID 'about'
);

So you could link to go.php?id=home for the homepage. However, this means you'd have to change the method by which you detect whether the specified ID is valid. You'd have to use something like:

if (array_key_exists($_GET['id'], $pages))
{
    // Valid id
} else {
    // Invalid ID
}

Hope this helps :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.