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)
LVL 2
beridiusAsked:
Who is Participating?
 
amiguraConnect With a Mentor Commented:
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

0
 
Mark BradyPrincipal Data EngineerCommented:
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

<?php
$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...

<?php
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?

?>
0
 
Ray PaseurCommented:
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.