0

I have an internal network running XAMPP. I am trying to set a cookie for an item when someone logs in. I use the following code. $Access will be a 1 if user authenticates correctly. When a user enters their username and password it passes to a CheckAuth page with the following code.

$Access = authenticateUser($_POST['Username'],$_POST['Password']);
setcookie("Access", $Access, time()+3600);
header('Location:Newpage.php');

On the NewPage it if I do a $_COOKIE['Access'] it will show me 1 and set everything correctly. If I click a link to another page, say Inventory.php, within the same folder, $_COOKIE['Access'] shows 0 instead of 1.

What could be the issue that the cookie is magically gone? I have checked the IE files for the cookie when it is made and it doesn't show up.

JukEboX
  • 355
  • 1
  • 3
  • 16
  • Does this answer your question? [Accessing $\_COOKIE immediately after setcookie()](https://stackoverflow.com/questions/3230133/accessing-cookie-immediately-after-setcookie) – JAAulde Jun 18 '20 at 18:04
  • @JAAulde that link does not help me. I am setting a cookie with the correct code but it can't doesn't actually set. Meaning I am trying to set the cookie on this page. And then on the next page I recall it and it should have 1. Instead it has nothing. – JukEboX Jun 18 '20 at 19:32

2 Answers2

1

The super global, $_COOKIE, only contains cookies that were given by the browser to the server in the incoming Request.

setcookie() only sets up HTTP headers for the outgoing Response that will instruct the browser to set a cookie.

If you want this cookie to be available in $_COOKIE, you'll need to set it manually at the same time as you call set_cookie():

setcookie('Access', $Access);
$_COOKIE['Access'] = $Access;
JAAulde
  • 19,250
  • 5
  • 52
  • 63
  • So if I set it with $_COOKIE['Access'] and then try and check it on a page in the same folder it says $_COOKIE['Access'] is 0. Does it not pass through pages? – JukEboX Jun 18 '20 at 19:11
  • You have to do both – JAAulde Jun 18 '20 at 19:55
  • I have done both and the cookie does not hold. I don't even see the cookie in the internet files where it would be stored. – JukEboX Jun 18 '20 at 20:17
-1
$Access = authenticateUser($_POST['Username],$_POST['Password']);

Missing a quote after $_POST['Username

$Access = authenticateUser($_POST['Username'],$_POST['Password']);
ChiefKeith
  • 83
  • 7