Unable to set Cookie in PHP

I set the cookie through setcookie() function, but when I was trying to refresh it, my custom message appears not set but same when I check through incognito mode in chrome, cookie successfully set, is it due to the cache issue?

have you checked for the ‘header already sent’ error?

Yeah there is no such error.

When debugging cookies, always use your browser’s developer console. in fact it’s the only facility that can help.
in the Network tab check the request and its headers. Look for both request and response and see whether there is a cookie or caching or anything.

Edit: Now works, have to change the permission of the folder.

I need one more help, while storing session in different custom path, I am getting this error

 Warning: session_start(): 
open(/Applications/XAMPP/xamppfiles/htdocs/pp/sessions/sess_m5krp93dic4tqe5c7asj1qlf64, O_RDWR) failed: Permission denied (13) in 
/Applications/XAMPP/xamppfiles/htdocs/pp/index.php on line 5

This is my simple code

<?php

session_save_path(__DIR__.DIRECTORY_SEPARATOR.'sessions');
session_name('customSessionName');
session_start();

the file permissions for the session data files are not sufficient.

Now WTF is going on is whenever I create custom session ID, it generate the new session file but now I can’t able to store anything over there due to the permission problem, I always have to edit the permission, this is ridiculous and headache, I am the admin and I don’t even have the file permission. :smashy:

You should learn Linux then because Windows is for beginners. Most Windows users don’t understand how the permissions in PHP work because PHP uses *gnix syntax.

There are 3 types of users that can access the file hence why you have 3 r’s, 3 w’s, and 3 x’s. They all read read-write-execute. Linux systems are very reliant on permissions. If you don’t have sufficient permissions, it will deny it whether you are an admin or not. This is where it branches off and Windows users get lost. In Windows, if you are admin, you can do anything. But there comes a point where you can’t edit files. This is where having knowledge of Linux comes in handy.

There is a reason why there are 3 of each letter.

  • First letter belongs to the author or creator of the file.
  • Second letter belongs to groups or people belonging to a group of the created file.
  • Third letter belongs to “others” in which anyone can access the files.

Having all 3 letters set to 777, you give full permissions for all groups. There is more, but my time right now is limited as I am on break but heading back to work right now.


EDIT: Sorry, I was wrong while I was typing that up. Was trying to type quick before I had to work. The first set of letters is for root/ file system. The second set of letters is for the author/ users in the group that the author is in. And the third set of letters is for others or other groups that don’t belong with the author. This in turn gives us a triple digit number. Here is a link to describe what each digit represents and how hey are totaled. https://www.cyberciti.biz/faq/unix-linux-bsd-chmod-numeric-permissions-notation-command/

Since it seems you are also using Mac, you should know that even though you are an admin, you do not have permissions to all files as if you would when you’re on Windows. This is why I said most Windows users won’t understand this because *gnix systems rely heavily on permissions. The only account that can actually do that is root and some *gnix systems don’t allow users to log in with root access. Only allow them to execute via terminal with root permissions.

I recently started using MAC, so I don’t know much about permission details for mac. Right now, can you tell me what should I do to get all the permission and how can I setup this permanently through terminal?

File permissions works same way in Mac (OS X) as they do in Linux.

You might need to be root user depending on what permissions you are about to change.

There really is no permanent way if the parent directory of the stored location isn’t set to give full read-write-execute. I am not really sure what /Applications holds so depending on what it has, there might not be a permanent way. You’ll always need to change it every time you create new files. Reason why I say that is because certain folders by default need to have a certain permission set. If the permission is wrong, your file system gets corrupted or you won’t be able to properly use your system. So if Applications has files in there by default, I suggest not touching them so changing permissions for /Applications cannot be done. However, changing permissions on /Applications/XAMPP is possible. However, this means that every time you create new files, the new files will by default have /Applications permission. It won’t have /Applications/XAMPP permission since the actual parent isn’t /Applications/XAMPP. Well it is, but it goes by the initial first directory that within the file system which is /Applications. Basically, when you create new files, you’ll also have to apply the same permissions to them in order to get that full access that you want. Since we are only dealing with this locally, you can set the permission to 777. It wouldn’t really matter. When you upload it to a live server, I suggest not setting the permissions like that because anyone can execute and overwrite your files. When we do it locally, unless if your computer is compromised or someone is using your network and hacked into your Mac, setting the permissions we want wouldn’t really matter.

So the command usually refers to chmod since it is used for changing permissions. We’ll also need to run this command using root permissions or it won’t work. Then, we’ll add in the -R flag which means recursive. This in turn will apply the same permissions on all sub-files and sub-directories. Then, we define the amount of permissions we want, in this case, we will set it to 0777. And last, we define which directory to set the permissions on. So, the command looks like this

sudo chmod -R 0777 /Applications/XAMPP

Then, you’ll need to enter your password for your current account so that you can have root permission.

Check the permission on that directory afterwards. I suggest changing the directory in the terminal to /Applications/XAMPP first. The final commands will be

sudo cd /Applications/XAMPP
ls -l

This will basically do just exactly what I explained. ls -l will show every file within /Applications/XAMPP and show what permissions those files and directories have. You should see that they all have read-write-execute permissions including /Applications/XAMPP. Refresh your localhost and it should be working.


@TeNDoLLA
That is correct. Linux and Mac both share the same hierarchy because Mac is unix. It runs bash just like Linux does and has very similar commands that Linux has. However, not all commands that Linux has are available on Mac.

1 Like

What I have done, I just fucked up the whole project folder to everyone and now everything works fine, which means now 777 :pensive:

I don’t understand what you mean by this. Please elaborate.

haha… I mean I have set the folder and sub folder to 777.

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