miniramen Posted May 25, 2010 Share Posted May 25, 2010 Hello, Recently, I'm asked to check URLs scripts for a redirect code, to see if the job is done or not by other people. So lets say I have like 100 urls to check. Thing is, how am I supposed to be able to check multiple url, go inside the script and find if there's a redirect code on it? Then I have to echo a list that will tell me if the code is there or not (So I guess I get a Bool value) from it. Do I need Regex for this application? I know that I'll need While (READ URL) { Check for redirect code; Echo if it exists or not; }; Any help would be amazing to get me into the right direction Thank you again!!!! Quote Link to comment https://forums.phpfreaks.com/topic/202829-check-redirect-code-from-multiple-url/ Share on other sites More sharing options...
premiso Posted May 25, 2010 Share Posted May 25, 2010 You have the right idea, but finding the redirect code is a bit of a task. You have to account for meta redirects, javascript redirects and potentially flash redirects. Javascript redirects can be on the main page or in a js file. So you would have to read those js files as well and parse them out. Even so a js redirect can be "encoded" and you would have to decode that set of strings to see if it does redirect and just because there is a .location directive does not necessarily mean that the current page is being redirected. So yea, just giving you a heads up to that part (as I have done this before). Regex would work for most of the tests so you may want to look into preg_match but you need to know if you need all redirects or just certain ones as it can get very complicated pretty quick. Quote Link to comment https://forums.phpfreaks.com/topic/202829-check-redirect-code-from-multiple-url/#findComment-1063095 Share on other sites More sharing options...
miniramen Posted May 25, 2010 Author Share Posted May 25, 2010 Hello, but I realized that the website url will change after it redirects. For example: www.url.com/15 <<<I click on that. www.url.com/25 <<<Now I get that. Is there a way to detect this change? Quote Link to comment https://forums.phpfreaks.com/topic/202829-check-redirect-code-from-multiple-url/#findComment-1063124 Share on other sites More sharing options...
premiso Posted May 25, 2010 Share Posted May 25, 2010 cURL maybe able to. But you will have to read up on that and its curl_setopt options as well to see if it is possible. Quote Link to comment https://forums.phpfreaks.com/topic/202829-check-redirect-code-from-multiple-url/#findComment-1063129 Share on other sites More sharing options...
miniramen Posted May 26, 2010 Author Share Posted May 26, 2010 Hello, yes I decided to use curl, and from that, I wanted to search for an ID number to see if the site is redirected to the right place. sadly, it seems the my code is unable to retrieve the content of the site that it's redirected to...actually, it can't even get any site content at all. Here's my code. fclose($file_open); foreach ($site_array as $site) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $site); $site_content = curl_exec($curl); echo $site; echo $sitecontent; // $site_content = file_get_contents($site); if (strpos($site_content, $redirectId) != false) echo $site . "," . "true" . "<br />" ; else echo $site . "," . "false" . "<br />" ; } Quote Link to comment https://forums.phpfreaks.com/topic/202829-check-redirect-code-from-multiple-url/#findComment-1063606 Share on other sites More sharing options...
miniramen Posted May 26, 2010 Author Share Posted May 26, 2010 Solved my own problem....but the thing is that now sometimes the script gives the right vlaue, and sometimes it does not...wtf? Please can someone tell me if this is code that is wrong? foreach ($site_array as $redirectId => $site) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $site); curl_setopt($curl, CURLOPT_RETURNTRANSFER, True); $site_content = curl_exec($curl); curl_close($curl); echo strpos(htmlentities($site_content), $redirectId) . ", "; echo $redirectId; /* $site_content = file_get_contents($site); preg_match('/$redirectId/', $site_content, $matches); print_r ($matches); */ if (strpos(htmlentities($site_content), $redirectId) != 0) echo $site . ", " . "true" . "<br />" ; else echo $site . ", " . "false" . "<br />" ; } fclose($file_open); ?> Sometimes, the script is unable to search through the website Quote Link to comment https://forums.phpfreaks.com/topic/202829-check-redirect-code-from-multiple-url/#findComment-1063664 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.