Angle bracket problem in simple display cookie

Hi,
I m trying to dispplay a cookie. I found some code. I modified it but its not running:

<?php

setcookie("name","value",time()+$int);

/*name is your cookie's name

value is cookie's value

$int is time of cookie expires*/

//echo '<script type="text/javascript">alert("Data has been submitted to ' . $to . '");</script>';

echo ‘<script> alert( “Testing” . $_COOKIE[“name”])</script>’;

?>

I am getting following message from nginx server:

2019/11/20 12:33:38 [error] 1214#1214: *3 FastCGI sent in stderr: “PHP message: PHP Parse error: syntax error, unexpected ‘>’ in /var/www/html/script_alert_cookie.php on line 13” while reading response header from upstream, client: 127.0.0.1, server: _, request: “GET /script_alert_cookie.php HTTP/1.1”, upstream: “fastcgi://unix:/var/run/php/php7.2-fpm.sock:”, host: “localhost”

Some body please guide me.

Zulfi.

Hi,
I changed the code. It had problems with inverted comma:

<?php

setcookie("name","value",time()+$int);

/*name is your cookie's name

value is cookie's value

$int is time of cookie expires*/

//echo '<script type="text/javascript">alert("Data has been submitted to ' . $to . '");</script>';

echo '<script> alert( "Testing" . $_COOKIE["name"])</script>';

?>

Now I am getting following error message:

2019/11/20 12:46:11 [error] 1214#1214: *7 FastCGI sent in stderr: “PHP message: PHP Notice: Undefined variable: int in /var/www/html/script_alert_cookie.php on line 3” while reading response header from upstream, client: 127.0.0.1, server: _, request: “GET /script_alert_cookie.php HTTP/1.1”, upstream: “fastcgi://unix:/var/run/php/php7.2-fpm.sock:”, host: “localhost”

Hi,
Now I am not getting any error message but nothing displayed:

<?php

setcookie("name","value",time());

/*name is your cookie's name

value is cookie's value

$int is time of cookie expires*/

//echo '<script type="text/javascript">alert("Data has been submitted to ' . $to . '");</script>';

echo '<script> alert( "Testing" . $_COOKIE["name"])</script>';

?>

Somebody please guide me.

Zulfi.

Is there more html code that you’re not showing?

Hi,
Thanks for your response. I am just running this Php code.
I found the code from internet.

I found that “alert” is javascript. One site says that Php does not execute it. It is displayed by browser. Is there any ‘time related cookie for browser’?

how can we modify the code to display some HTTP cookie

Zulfi.

<?php
ob_start();   
$cookie_name = 'user';
$cookie_value = 'John Doe';
setcookie($cookie_name, $cookie_value, time() + (86400 * 30), "/");
echo '<script>alert("Testing ' .$_COOKIE[$cookie_name] . '");</script>';

Danger Will Robinson, Danger!

Manage expectations.
setcookie does not modify the $_COOKIE superglobal array (which is declared at parse time). The echo will return nothing on the first page load.

The code I posted works on “the first page load”.

Didn’t for me. It echoed "Testing ".

Check your cookies.

Ok, I see what you mean. Your right. Fixed it.

The error message states that there is an undefined integer variable $int

Try setting a default to $int=-1 before calling setcookie(...);

Even better

<?php 
// Good practise to be aware of what is happening
// following three lines should detect all errors, warnings and even notifications!
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors','1');

// fixed over enthusiastic iPad predictive text thanks @benanamen 
if ( isset($int) ) : 
// if ( is set($int) ) : 
  // No problem because this file must haves inherited a value
else : 
  $int = -1;
endif; 
setcookie("name","value",time()+$int);

Typo :grinning:

1 Like

When the time of the cookie is less that now, the cookie will be deleted from the browser. So -1 might not be the best default value for $int

1 Like

I just wondered if you need to do all the normal doctype and html stuff before a <script> tag will actually work. Perhaps it needs to follow a doctype, and be inside html / body tags.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.