Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 314
  • Last Modified:

php cookie

I need some code to:

when a user logs on the site they get a page that is not the main page (info.php)
but when the revist the site the dont get the page(info.php)
1 Solution
don't really understand. sound like nobody can view info.php

when user logs in they see info.php once after that they don't see it again.
am i right?

setcookie("info", 1, time()+31449600);
if($_COOKIE['info']!=1){ // show file}

// example if($_COOKIE['info']!=1){ echo '<a href="info.php">info</a>';}

Open in new window

Mark BradyCommented:
Can you try and re ask your question. What is it that you are trying to do? Do you want to set a cookie to let you know if a user has been to your site before or not?

If so, then a simple cookie to remember the users name would be something like this. For this example you would need to know their username and would be kept in the variable $username

$value = $username;
$cookieName = 'visitor';
setcookie($cookieName, $value, time()+3600*24*365);  /* expires in 1 Year */

The above code will write and save a cookie on the users computer. The cookie name in this case will be 'visitor'. You can name this whatever you like. The cookie expires in 1 year. you can modify the code to make it any length you want or if you want to delete the cookie, set a date in the past and it will be deleted as soon as the browser is closed.

All it does is store the $username variable/value. To get the data in the cookie on another page or your main page you use this code...

if (isset($_COOKIE['visitor'])) {
// get the users name here
$username = $_COOKIE['visitor'];

Now you have the username in the $username variable when they revisit your website.

Is this the sort of thing you are trying to achieve?

Ray PaseurCommented:

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now