Cookies are small files that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. PHP can create and retrieve cookie values.
<?php
// Setting a cookie
setcookie("user", "John Doe", time() + (86400 * 30), "/");
echo "Cookie named 'user' is set!";
?>
Use setcookie()
to set a cookie in PHP. The function requires the name, value, and expiry time of the cookie.
Console Output:
Cookie named 'user' is set!
Once a cookie is set, you can access it using the $_COOKIE
superglobal array in PHP.
<?php
if(isset($_COOKIE["user"])) {
echo "User is " . $_COOKIE["user"];
} else {
echo "User is not set";
}
?>
Always check if a cookie is set using isset()
before accessing its value to avoid errors.
Console Output:
User is John Doe
The expiry time is specified in seconds. A cookie will expire after the set time from when it is created.
<?php
// Cookie expires in one hour
setcookie("user", "John Doe", time() + 3600, "/");
echo "Cookie with one hour expiry set!";
?>
Use the time()
function to set the expiry time dynamically.
Console Output:
Cookie with one hour expiry set!
To delete a cookie, use the setcookie()
function with an expiry time in the past.
<?php
// Deleting a cookie
setcookie("user", "", time() - 3600, "/");
echo "Cookie 'user' is deleted!";
?>
Setting the expiry time to a past date effectively deletes the cookie.
Console Output:
Cookie 'user' is deleted!
To ensure cookies are only sent over secure connections, set the secure
parameter to true
.
<?php
// Secure cookie
setcookie("secure_user", "Jane Doe", time() + (86400 * 30), "/", "", true, true);
echo "Secure cookie 'secure_user' is set!";
?>
The HttpOnly
flag prevents JavaScript from accessing the cookie, enhancing security.
Console Output:
Secure cookie 'secure_user' is set!
The path
and domain
parameters define the scope of the cookie. The cookie will be available within the specified path and domain.
<?php
// Cookie with specific path and domain
setcookie("site_user", "Alice", time() + (86400 * 30), "/example/", "example.com");
echo "Cookie 'site_user' is set for example.com!";
?>
Setting the path to /
makes the cookie available throughout the entire domain.
Console Output:
Cookie 'site_user' is set for example.com!
Browsers generally limit the size of a cookie to 4096 bytes. Exceeding this size may cause issues with cookie storage and retrieval.
<?php
// Example of a large cookie
$largeValue = str_repeat("a", 4096);
setcookie("large_cookie", $largeValue, time() + (86400 * 30), "/");
echo "Large cookie is set!";
?>
Consider using sessions or server-side storage for larger data to avoid exceeding cookie size limits.
Console Output:
Large cookie is set!
Cookies are domain-specific and cannot be shared across different domains. However, subdomains can share cookies if the domain is set appropriately.
<?php
// Cookie for subdomain sharing
setcookie("shared_user", "Bob", time() + (86400 * 30), "/", ".example.com");
echo "Cookie 'shared_user' is set for all subdomains of example.com!";
?>
Set the domain with a leading dot (e.g., .example.com
) to share cookies across subdomains.
Console Output:
Cookie 'shared_user' is set for all subdomains of example.com!
Always use the secure
and HttpOnly
flags for cookies that contain sensitive information, and consider using the SameSite
attribute to prevent CSRF attacks.
<?php
// Secure cookie with SameSite attribute
setcookie("secure_session", "session_data", time() + (86400 * 30), "/", "", true, true, ["SameSite" => "Strict"]);
echo "Secure session cookie with SameSite is set!";
?>
The SameSite
attribute helps mitigate the risk of cross-site request forgery (CSRF) attacks by allowing you to declare if your cookie should be restricted to a first-party or same-site context.
Console Output:
Secure session cookie with SameSite is set!
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies