zm455 zm455 - 11 months ago 61
PHP Question

Query string is appending when using $_SERVER['HTTP_REFERER']

Can someone explain me why when I execute this code multiple times it append the substring

to my url?

Example: My script is located in index.php and if I execute it 3 times I will have this url in my browser:

I would like to have
. . .

if(isset($_POST['hidden']) && $_POST['hidden'] == 2){
header("location: $page?val=1");
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<input name ="hidden" value="2">
<input type="submit">

Answer Source

Just add a test before appending ?val=1

if(isset($_POST['hidden']) && $_POST['hidden'] == 2){
    $page = $_SERVER['HTTP_REFERER'];
    if(strpos($page, '?val=1') === false) $page .= '?val=1';
    header("location: $page");
    exit; // Avoid further execution if more code is below this.