in Education by
I'm trying to code a redirect checker, to check if a URL is search engine friendly. It has to check if a URL is redirected or not, and if it's redirected it has to tell if it's SEO friendly (301 status code) or not (302/304). Here's something similiar I've found: http://www.webconfs.com/redirect-check.php It also should be able to follow multiple redirects (e.g. from A to B to C) and tell me that A redirects to C. This is what I got so far, but it doesn't work quite right (example: when typing in www.example.com it doesnt find the redirect to www.example.com/page1) <?php // You can edit the messages of the respective code over here $httpcode = array(); $httpcode["200"] = "Ok"; $httpcode["201"] = "Created"; $httpcode["302"] = "Found"; $httpcode["301"] = "Moved Permanently"; $httpcode["304"] = "Not Modified"; $httpcode["400"] = "Bad Request"; if(count($_POST)>0) { $url = $_POST["url"]; $curlurl = "http://".$url."/"; $ch = curl_init(); // Set URL to download curl_setopt($ch, CURLOPT_URL, $curlurl); // User agent curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // Include header in result? (0 = yes, 1 = no) curl_setopt($ch, CURLOPT_HEADER, 0); // Should cURL return or print out the data? (true = return, false = print) curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Timeout in seconds curl_setopt($ch, CURLOPT_TIMEOUT, 15); // Download the given URL, and return output $output = curl_exec($ch); $curlinfo = curl_getinfo($ch); if(($curlinfo["http_code"]=="301") || ($curlinfo["http_code"]=="302")) { $ch = curl_init(); // Set URL to download curl_setopt($ch, CURLOPT_URL, $curlurl); // User agent curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]); // Include header in result? (0 = yes, 1 = no) curl_setopt($ch, CURLOPT_HEADER, 0); // Should cURL return or print out the data? (true = return, false = print) curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Timeout in seconds curl_setopt($ch, CURLOPT_TIMEOUT, 15); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Download the given URL, and return output $output = curl_exec($ch); $curlinfo = curl_getinfo($ch); echo $url." is redirected to ".$curlinfo["url"]; } else { echo $url." is not getting redirected"; } // Close the cURL resource, and free system resources curl_close($ch); } ?> http:// / e.g. www.google.com
JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
Well if you want to record every redirect you have to implement it yourself and turn off the automatic "location following": function curl_trace_redirects($url, $timeout = 15) { $result = array(); $ch = curl_init(); $trace = true; $currentUrl = $url; $urlHist = array(); while($trace && $timeout > 0 && !isset($urlHist[$currentUrl])) { $urlHist[$currentUrl] = true; curl_setopt($ch, CURLOPT_URL, $currentUrl); curl_setopt($ch, CURLOPT_HEADER, true); curl_setopt($ch, CURLOPT_NOBODY, true); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false); curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); $output = curl_exec($ch); if($output === false) { $traceItem = array( 'errorno' => curl_errno($ch), 'error' => curl_error($ch), ); $trace = false; } else { $curlinfo = curl_getinfo($ch); if(isset($curlinfo['total_time'])) { $timeout -= $curlinfo['total_time']; } if(!isset($curlinfo['redirect_url'])) { $curlinfo['redirect_url'] = get_redirect_url($output); } if(!empty($curlinfo['redirect_url'])) { $currentUrl = $curlinfo['redirect_url']; } else { $trace = false; } $traceItem = $curlinfo; } $result[] = $traceItem; } if($timeout < 0) { $result[] = array('timeout' => $timeout); } curl_close($ch); return $result; } // apparently 'redirect_url' is not available on all curl-versions // so we fetch the location header ourselves function get_redirect_url($header) { if(preg_match('/^Location:\s+(.*)$/mi', $header, $m)) { return trim($m[1]); } return ""; } And you use it like that: $res = curl_trace_redirects("http://www.example.com"); foreach($res as $item) { if(isset($item['timeout'])) { echo "Timeout reached!\n"; } else if(isset($item['error'])) { echo "error: ", $item['error'], "\n"; } else { echo $item['url']; if(!empty($item['redirect_url'])) { // redirection echo " -> (", $item['http_code'], ")"; } echo "\n"; } } It's possible that my code isn't fully thought out, but I guess it's a good start. Edit Here's some sample Output: http://midas/~stefan/test/redirect/fritzli.html -> (302) http://midas/~stefan/test/redirect/hansli.html -> (301) http://midas/~stefan/test/redirect/heiri.html

Related questions

0 votes
    Why are HTTP redirects significant? (a) TCP connection available (b) Complete roundtrip absent (c) Complete ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    We switched from citrix to HAProxy for load balancing recently. The Problem is that for some requests ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 23, 2022 in Education by JackTerrance
0 votes
    I want to get the size of an http:/.../file before I download it. The file can be a webpage ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    My application sends email using Microsoft Graph. We followed the documentation, this was working as expected for ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 17, 2022 in Education by JackTerrance
0 votes
    http://www.google.com/search?q=talkjarvis In the above URL which one is the argument which is used ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    HTTP defines two ways in which values entered by a user at the browser can be sent to the Web ... Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    What temporarily redirects response to the browser? (a) (b) (c) response.sendRedirect(URL) (d) response.setRedirect( ... & API of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
    What temporarily redirects response to the browser? (a) (b) (c) response.sendRedirect(URL) (d) response ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 24, 2021 in Education by JackTerrance
0 votes
    How can we POST JSON data with cURL?...
asked Jan 11, 2021 in Technology by JackTerrance
0 votes
    Can you exclude headers and footers from the input files before loading the data?...
asked Mar 23, 2021 in Technology by JackTerrance
0 votes
    I have yet another "Can't set headers after they are sent." problem. I've created a post router ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 30, 2022 in Education by JackTerrance
0 votes
    In Twilio Rest API for each call there is a response which uniquely identify calls Twilio-Request-Id: ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 19, 2022 in Education by JackTerrance
0 votes
    I've got a Debian Etch system running Exim4-daemon-heavy. The system is open to the internet, but ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    I want to get a list of the column headers from a pandas DataFrame. The DataFrame will come from user input so I don ... 'gdp', 'cap'] Select the correct answer from above options...
asked Jan 26, 2022 in Education by JackTerrance
...