0

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?

Domeniko
  • 109
  • 2
  • 3
  • 8
  • 1
    From the docs: "*Once the cookies have been set, they can be accessed on the **next page load** with the `$_COOKIE` array.*" http://php.net/manual/en/function.setcookie.php – Qirel Jun 17 '16 at 16:51
  • 2
    Store the ID in session, so you can use it in any page : `$_SESSION[ "id" ] = $_GET[ "id" ];` , you need no forms or cookies (don't forget `session_start();` in every page!). – Jose Manuel Abarca Rodríguez Jun 17 '16 at 16:52
  • "*I assume I need to set and store the cookie per session (so that cookie will refresh after session / browser exit)*" Yes, a session would be destroyed when the browser closes, and a cookie can have a longer lifespan. If you want to use the content of the cookie on that page, before reloading, just use `$affid`, and after the refresh of the page, or accessing another page, you can use `$_COOKIE['affid']`. – Qirel Jun 17 '16 at 17:00

2 Answers2

1

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"].

index.php

Url: www.domain.com/?id=user

    <?php
         session_start();
         if (isset($_GET["id"])) {
              $_SESSION["id"] = $_GET["id"];
    }
?>

otherpage.php

Url: www.domain.com/otherpage.php

<?php
session_start();
if (isset($_SESSION["id"])){
echo $_SESSION["id"];
}
?>
  • Thanks for the help guys. $_SESSION["id"] = $_GET["id"]; This does show up when I echo it on the initial page load, however, when I navigate to some other page, it does not echo anything, as its blank / null for some reason. – Domeniko Jun 17 '16 at 17:02
  • @ArchieVigaca All pages trying to use `$_SESSION` must call `session_start();` prior to any output, on *every page* trying to use the sessions. – Qirel Jun 17 '16 at 17:03
  • You have to call `session_start();` on that page also – Buildersrejected Jun 17 '16 at 17:03
  • session_start() is included on all pages. The whole code snippet is included on all pages including $_SESSION['id'] = $_GET['id'];. Can this be a problem? Does it reset the session ID value? If yes, how could I fix that? – Domeniko Jun 17 '16 at 17:09
  • 1
    Do not, include the ` =$_GET["id"];`on every page this overwrites the variable. Setting it only once is fine – Buildersrejected Jun 17 '16 at 17:17
  • 2
    Yes, that overwrites it each time the page is loaded. You can wrap it in a condition `if (!isset($_SESSION['id'])) { $_SESSION['id'] = $_GET['id']; }` or just have it on one of the pages. – Qirel Jun 17 '16 at 17:17
  • Hi @Buildersrejected can you please give me a code snippet how to set the id once so that it wont overwrite on other pages? – Domeniko Jun 17 '16 at 17:17
  • Thanks Qirel. Got it working. Thank you everyone. Appreciate your help a lot! : )) – Domeniko Jun 17 '16 at 17:20
1

Jose is right about saving IDs in sessions. There's a good post about it that deals SPECIFICALLY with IDs here: Cookie vs Session for Storing IDs

But, if you want to store it as a cookie, this code stores the ID.

$id  = $_GET['id']);
setcookie('id', $id);

And this code allows you to retrieve the ID!

echo $_COOKIE['id'];
Community
  • 1
  • 1
Ray Li
  • 7,601
  • 6
  • 25
  • 41