Jump to content

Redirect


RobG

Recommended Posts

I am new to working with php, so forgive me if this is rudimentary ...

 

I am trying to create a redirect script.

 

The crux of this is as follows.

 

I have a database that houses a specific identifier (Provider ID) and a url that is associated with the ID

 

On a web form I have a field to type in the provider id and a submit button

 

What I am trying to do is have the user enter the provider ID into the field, hit submit, and then have the script match the ID to the url associated with it and have it redirect the user to the correct website.

 

I have been searching all over to find something that fits the bill, but no luck.

 

Seems to me this should be straight forward.

 

Any help would be most appreciated

 

Rob

Link to comment
Share on other sites

At this point all I have been trying to do is actually FIND something that will point me in the right direction.  

 

All I have found thus far is a script where I can define the url to redirect to directly in the script.  What I am needing is for the form to define what ID to search for and then have the script match it in the database and redirect.

 

Some general direction where to search would be appreciated

Link to comment
Share on other sites

No idea if have any current code, the form names and values data types...form method type are using.

 

Use the $_GET['id'] or $_POST['id'] value from your form.

If your id's are a digit can check with ctype_digit()

Do your query to obtain their website url.

Use a header redirect with the url value.

header('Location: http://www.example.com/');
exit;

Something like this, Do this code top of the script and do not output anything before it.

 

PDO with prepared statements used and written so can modify a bit to your needs, in case need to check something or add more columns.

(use your credentials and values of course)

<?php
if (isset($_POST['id']) && trim($_POST['id']) != '') {
    
    $id = $_POST['id'];
    
    try {
        $db    = new PDO("dbtype:host=yourhost;dbname=yourdbname;charset=utf8", "username", "password");
        $query = $db->prepare('SELECT url FROM providers_table WHERE id = :provider_id');
        $query->bindParam(':provider_id', $id);
        $query->execute();
        while ($row = $query->fetchAll(PDO::FETCH_ASSOC)) {
            $url = $row['url'];

            if ($url) {
                
                header('Location: ' . $url);
                exit;
                
            }
            
            
        }
    }
    catch (PDOException $e) {
        echo "Error: " . $e;
    }
}
?> 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.