URL重定向基于输入,但如果输入URL不存在,则不重定向无法使PHP工作

URL Redirect based on input but if input url does not exist do not redirect cant get PHP to work

本文关键字:URL 输入 重定向 工作 PHP 于输入 如果 不存在      更新时间:2023-09-26

@themask你好,谢谢你的帮助,我已经打开了php,现在只有这个代码

<script>
document.forms[0].onsubmit = 
    function() {
       var to = document.getElementById('myInput').value;
       var ajax = new XMLHttpRequest; 
       ajax.onreadystate = function() {
            if(this.readyState == 4 && this.status != 404) {
                window.locaiton.replace(to);
            } else {
                window.location.replace('http://www.mysite.com/incontinence/protective_underwear/presto_protective_underwear/');
            }
    }; 
    ajax.open('GET',to);
    ajax.send(null);
};
      </script>
      <form onsubmit="location.href='http://www.mysite.com/coupons/' + document.getElementById('myInput').value; return false;" >
  <input type="text" id="myInput" />
  <input name="Submit" type="submit" id="Submit" />
</form>

但如果使用了错误的代码,它仍然会将我发送到不正确的URL。这就像是一起跳过java。

任何额外的帮助都将是伟大的。

谢谢,云母

谢谢,云母

尝试使用get_headers((

$headers = get_headers('http://www.activelifemed.com/incontinence/protective_underwear/presto_protective_underwear/')
if($headers[0] == 'HTTP/1.1 200 OK'){
  //exists
}
else{
  //doesn't exist
}

http://www.php.net/manual/en/function.get-headers.php

您可以使用JavaScript:

document.forms[0].onsubmit = 
    function() {
       var to = document.getElementById('myInput').value;
       var ajax = new XMLHttpRequest; 
       ajax.onreadystate = function() {
            if(this.readyState == 4 && this.status != 404) {
                window.locaiton.replace(to);
            } else {
                window.location.replace('http://www.mysite.com/incontinence/protective_underwear/presto_protective_underwear/');
            }
    }; 
    ajax.open('GET',to);
    ajax.send(null);
};