I would like to know if i can redirect my web page to an external web page from a Php switch statement without the use of html (purely Php). I have made a username/password login using the $_POST predefined variable which is linked to a switch statement which from there depending on what is inputted will auto redirect the user to an external web page. I do not want to have to click on an echoed link, i would much rather have the web page auto directed. Can someone please tell me if this is possible or what other method should I use.
Asked
Active
Viewed 703 times
3 Answers
1
Try this code. this would definetely help you.
<?php
$location = 'https://www.google.co.in'; //change url as per your requirement
header('Location:' . $location);
?>

Makwana Ketan
- 1,380
- 1
- 14
- 22
1
Use:
header("Location:pathtoTheExternalwebpage");
Make sure you don't echo
any html
codes or anything in the web browser before using this statement.(Not even a space ' ')

Anish Silwal
- 980
- 1
- 10
- 24
1
It's easy. I have written a very simple function for you, here it goes:
<?php
function redirect($source) {
switch ($source) {
case 'login':
header("Location: login.php");
break;
default:
header("Location: {$source}");
break;
}
}
?>
Usage:
If you will call function like redirect('login');
, it will redirect to login.php
or if you will call it like redirect('http://externalpage.com');
, it will redirect to the external URL specified.
Specify your own conditions to this function and you can add more functionality easily.

Rehmat
- 4,681
- 3
- 22
- 38