if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == 'http://example.com/previous-page'){ // process the form data } else { // redirect the user back to the previous page header('Location: ' . $_SERVER['HTTP_REFERER']); exit(); }
function checkReferer(){ if(isset($_SERVER['HTTP_REFERER'])){ $referer = parse_url($_SERVER['HTTP_REFERER']); if($referer['host'] == 'example.com'){ return true; } } return false; } if(checkReferer()){ // allow access to the resource } else { // deny access to the resource }The PHP Controller referer method is included in the PHP core library and no external package or library is required to use it.