PHP检测重定向到自我

Is it possible to detect a redirect in php, if the redirect was to itelf?

in breif; can I add a line or two of code to the top of my page to first check if the page is a redirect. If it is act on it?

PHP Final line excuted on submit

header("Location: " . $_SERVER['REQUEST_URI']);

HTML Form

<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>?submit=true" method="POST">

Still green with php, so learning little by little. Basically I need to control some variables if the page was redirected but only if.

If you want to prevent form resubmisson on page refresh, then your solution really, really should be to POST the form data to another page that saves the data and then uses a header("Location:...) back to the form. The user will be none the wiser and the refreshing of the form page will never cause a resubmission.

As a bonus this would also mean you would no longer need to use PHP_SELF. As a piece of bonus advice it's usually far better to use "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'] as PHP_SELF can have extra data added by the browser / user which can cause you issues.

It is also worth reading up on how to defend yourself against CSRF attacks.

Put this in your script and have a look at it's content;

<pre>
<?php print_r($_SERVER); ?>
</pre>

This way you can find out which keys contain 'what' you can get which index has the referer or not and write your If Condition

you can use the $_SERVER['HTTP_REFERER'] to get the referer like

if(isset($_SERVER['HTTP_REFERER'])){
    $cur_p="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']
    if($_SERVER['HTTP_REFERER']==$cur_p){
        //add your code here you want to do what if current == referer
    }
} 

but HTTP_REFERER index is not always set so you can also use javascript like

if(document.referrer == location.href){
    //add your code
}

let me know if this was help full