user1970146
user1970146

Reputation: 1

Redirect if a specific word is present in a php variable

I am making a web hosting website. I have a text field where a user can put in the domain name they want to register for example "domain.com" or "domain.info". I want to redirect the domains which have .info in their url to a different page for example www.example.com/info.php and domains which have .com in their url to www.example.com/com.php, is it possible to do that using php?

Your help will be highly appreciated.

Thanks

Upvotes: 0

Views: 48

Answers (2)

Ferry Kobus
Ferry Kobus

Reputation: 2029

Yes it's possible.

For example your input is called name="domain"

in php you catch the post:

if($_SERVER["REQUEST_METHOD"] == "POST") {
    $extension = end(explode($_POST["domain"]));
    header("Location: ".$extension.".php");
}

This wil redirect the extension with .php

Be aware, this is not secure! But just a push in the right direction.

A more secure option:

if($_SERVER["REQUEST_METHOD"] == "POST") {
    $extension = end(explode($_POST["domain"]));
    switch($extension) {
        case 'com':
            $redirect = 'com.php';
            break;
        case 'info':
            $redirect = 'info.php';
            break;
        default :
            $redirect = 'unknown.php';
            break;
    }
    header("Location: ".$redirect);
}

Upvotes: 1

SeanWM
SeanWM

Reputation: 16989

Something like this:

if(isset($_POST['domain'])){
   $arr = explode('.', $_POST['domain']);
   if($arr[1] == 'com'){
       // redirect
   }elseif($arr[1] == 'info'){
       // redirect somewhere else
   }
}

This is just a guideline. You should escape the $_POST data.

Upvotes: 0

Related Questions