RobG Posted February 11, 2016 Share Posted February 11, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/300789-redirect/ Share on other sites More sharing options...
Jacques1 Posted February 11, 2016 Share Posted February 11, 2016 What's your current code? What did you try? Quote Link to comment https://forums.phpfreaks.com/topic/300789-redirect/#findComment-1531016 Share on other sites More sharing options...
RobG Posted February 11, 2016 Author Share Posted February 11, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/300789-redirect/#findComment-1531017 Share on other sites More sharing options...
Jacques1 Posted February 11, 2016 Share Posted February 11, 2016 It's difficult to help when you don't provide any information about your current knowledge and the specific problems you have with the task. What kind of help are you looking for? A basic “Hello World” tutorial which explains how to process form data in PHP? A database tutorial? Something else? Quote Link to comment https://forums.phpfreaks.com/topic/300789-redirect/#findComment-1531021 Share on other sites More sharing options...
QuickOldCar Posted February 11, 2016 Share Posted February 11, 2016 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/300789-redirect/#findComment-1531030 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.