user2869402 user2869402 - 15 days ago 5
PHP Question

how to GET url values after header function?

Peace be with you all,
I have a small problem which is i don't know how to store the values i get from a url using

$_GET
method after using
header
function, here is the code so you can get what i am saying:-

i have index.php that has the link with the values:-

<?php
echo "<a href='test.php?id=5&s=6'>Click Here</a> <br>";
?>


Here is test.php which is supposed to get the values from this url:-

<?php

if(isset($_GET['id'])&&isset($_GET['s'])){
echo $_GET['id'].$_GET['s'];
header('location:test.php');
}
?>


I don't know how to get the values from the url along with using the
header
function, i get them normally when i don't use the
header
function what can i do to store the variables to use them after redirecting using the
header
function? thanks in advance

Answer

You use session variables to store variables across pages. At the top of each page add session_start()....to decare the session variable then do this:

    if(isset($_GET['id']) && isset($_GET['s'])){
      $_SESSION['id'] = $_GET['id'];
       header('location:test.php');
       exit;
     }

Now, on your test.php file, if you add the session_start at the top of the page you can access that variable: $_SESSION['id'].

Comments