Archie Vigaca Archie Vigaca - 5 months ago 9
PHP Question

Get ID from URL and store it in variable

I need to get the data from URL, example domain.com/?id=username

Username will vary from one user to another... Once they visit the website with link like that, they can move around the website and then at some point fill out the form. Since they moved around the website, the url will not have ?id=username in the path, so I need to store that data in the variable to be able to send it with the form.

I assume I need to set and store the cookie per session (so that cookie will refresh after session / browser exit)

I use ob_start() since I have to implement this code in the body, when the headers are already sent.

ob_start();
session_start();
$affid = $_GET['id'];
setcookie('affid',$affid, 0, "/");
$finalaffID = $_COOKIE['affid'];
ob_end_clean();
echo '<span class="testoutput">'.$finalaffID.'</span>';


After some attempts, I got this code, but it doesnt store the value after I move around couple pages, it only shows the on initial page visit.

Any ideas please?

Answer

You could use session variables.

$_SESSION["id"] = $_GET["id"];

this session var will be accessible anywhere the session is open. Just call it with $_SESSION["id"].

Comments