0

i want to set cookies to the webpage, the webpage with extension .php has this content

<?php
setcookie("engineer","engineer",time()+3600);
?>
<html>
<img src= "image3.jpg"  height="300" width="300"/>
</html>

i created a apache2 server in my ubantu virtual machine, and placed the above file with name test.php in the /var/www/html folder and tried to access the website, the website got opened but the cookie was not set :( i don't the reason why cookie has not set. i tried some answers from stackoverflow but still not able to set cookie anyone help me in this Here is what i tried

  1. added the '/' for path

    setcookie("engineer","engineer",time()+3600,'/'); but still not able to setcookie

  2. uninstalled and reinstalled the apache2 server but still same issue.

  3. also tried by removing time() in that line setcookie("engineer","engineer",3600);

Aviroxi
  • 115
  • 1
  • 9
  • 1
    "cookie has not set" means that you didn't get the HTTP header or that the browser ignored it? – Álvaro González Feb 09 '21 at 17:44
  • header has came i mean the image attached was visible , but cookie was not set – Aviroxi Feb 09 '21 at 17:46
  • i found that cookie was not set up with cookie editor extension in the firefox and i also confirmed that the cookie was not set by performing wireshark packet capture – Aviroxi Feb 09 '21 at 17:48
  • you need to specify the domain and path, https://www.php.net/manual/en/function.setcookie.php you are missing some parts to setcookie() – Ryan H Feb 09 '21 at 18:01
  • @RyanH i read that php manual too, in the first example they didn't use the domain for that , and i tried giving the path which is '/' – Aviroxi Feb 09 '21 at 18:05
  • @RyanH many videos i saw in youtube they are using the same format i used but i could not get it :( – Aviroxi Feb 09 '21 at 18:07
  • @gurukiranx from the docs `Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expires parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);.` look at the actual network request in your browsers debugger and look at the header that was sent from your server to verify that the header was transmitted. – Ryan H Feb 09 '21 at 18:24
  • To set a baseline, [this is the HTTP header](https://i.imgur.com/0oEkREn.png) I see in my browser's Network pane (F12). It then shows up in the [cookie pane](https://i.imgur.com/HPPG6bD.png). And finally the browser sends a [different header on **next** requests](https://i.imgur.com/fbufKlb.png). Can you please go through these steps and see when your workflow stops working? – Álvaro González Feb 10 '21 at 09:46
  • @ÁlvaroGonzález thank you so much for your time but the main thing is i didn't install the php at all. i found the answer here https://stackoverflow.com/questions/5121495/php-code-is-not-being-executed-instead-code-shows-on-the-page – Aviroxi Feb 10 '21 at 12:12

1 Answers1

0

after so much of searching in internet i found the solution, the main thing you need to display a php page is first you need to install the php, to install php use this command

$ sudo apt-get install php

after installing php you need to type this command

$ sudo a2enmod proxy_fcgi setenvif

$ sudo service apache2 restart

here is reference

Aviroxi
  • 115
  • 1
  • 9