frank_solo Posted May 24, 2013 Share Posted May 24, 2013 I have a question I have page like so: index.php?id=100 but let's say that id=101 is blank Is there a way to redirect the user if that page is empty. Redirect them to homepage. Is this possible and how? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/278366-redirect-if-id-is-empty/ Share on other sites More sharing options...
requinix Posted May 24, 2013 Share Posted May 24, 2013 As soon as you discover the thing is "empty", use a header to redirect (check the example). Make sure you do so before there has been output of any kind, including HTML and whitespace before your <?php tags. Quote Link to comment https://forums.phpfreaks.com/topic/278366-redirect-if-id-is-empty/#findComment-1432149 Share on other sites More sharing options...
Q695 Posted May 25, 2013 Share Posted May 25, 2013 you can do that or do an if (!$id) on the inside, then display one page, or the other with includes. Quote Link to comment https://forums.phpfreaks.com/topic/278366-redirect-if-id-is-empty/#findComment-1432164 Share on other sites More sharing options...
frank_solo Posted May 25, 2013 Author Share Posted May 25, 2013 Well I've done this so far but this helps only if index.php?id=(blank) if (!isset($_GET['id']) || empty($_GET['id'])) { header('location: http://www.mydomain.co') ; } What I would like as well is to query the table and check if the id exist if not to redirect. Quote Link to comment https://forums.phpfreaks.com/topic/278366-redirect-if-id-is-empty/#findComment-1432168 Share on other sites More sharing options...
requinix Posted May 25, 2013 Share Posted May 25, 2013 So do that. Quote Link to comment https://forums.phpfreaks.com/topic/278366-redirect-if-id-is-empty/#findComment-1432171 Share on other sites More sharing options...
Q695 Posted May 25, 2013 Share Posted May 25, 2013 Is it solved, or do we need to help you further? Quote Link to comment https://forums.phpfreaks.com/topic/278366-redirect-if-id-is-empty/#findComment-1432181 Share on other sites More sharing options...
frank_solo Posted May 25, 2013 Author Share Posted May 25, 2013 This worked <?php session_start(); include 'config.php'; $url_id = mysql_real_escape_string($_GET['id']); $sql = "SELECT id FROM places WHERE id='$url_id' AND unit='Residential' AND county='New Jersey'"; $result = mysql_query($sql); if(mysql_num_rows($result) >0){ //found $query="UPDATE `places` SET `views` = `views` + 1 WHERE `id` = $url_id"; }else{ //not found header("location: http://www.mydomain.co"); } ?> But now this won't work$query="UPDATE `units` SET `views` = `views` + 1 WHERE `id` = $url_id"; Can someone show me how to make that statement work or should I start a different thread? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/278366-redirect-if-id-is-empty/#findComment-1432189 Share on other sites More sharing options...
frank_solo Posted May 25, 2013 Author Share Posted May 25, 2013 Got it: <?php include "config.php"; $id = mysql_real_escape_string((int)$_GET['id']); $sql = "SELECT id FROM places WHERE id='$id' AND unit='Residential' AND county='New Jersey'"; $result = mysql_query($sql); if(mysql_num_rows($result) >0){ //found }else{ //not found header("location: http://www.mydomian.co"); } $query = "UPDATE `units` SET `views` = `views` + 1 WHERE `id` = $id"; $res = mysql_query($query, $bd); $query = "SELECT * FROM `places` WHERE `id` = $id"; $res = mysql_query($query, $bd); $result = mysql_fetch_assoc($res); $lat = $result['lat']; $contact = $result['contact']; $phone = $result['phone']; $long = $result['lng']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/278366-redirect-if-id-is-empty/#findComment-1432194 Share on other sites More sharing options...
Solution requinix Posted May 25, 2013 Solution Share Posted May 25, 2013 (edited) header() does not immediately end your script - it will continue running. Use an exit; or die; after calling it. //not found header("location: http://www.mydomian.co"); exit; Edited May 25, 2013 by requinix Quote Link to comment https://forums.phpfreaks.com/topic/278366-redirect-if-id-is-empty/#findComment-1432203 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.