Loading files if cookie isn't found? + Rep for best answer!

Discussion in 'PHP' started by chriswick, Dec 20, 2007.

  1. #1
    Hey Everyone,

    I'm in need of a script that will load a file if it can't find a cookie on that persons computer and then write a cookie to that user, does anyone know of a script that could do this.

    + Rep for best answer! :D

    Thanks!
     
    chriswick, Dec 20, 2007 IP
  2. Meth_

    Meth_ Well-Known Member

    Messages:
    1,063
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    140
    #2
    
    <?php
    
    if(isset($_COOKIE['whatever'])){
    require_once('thefile.php');
    }else{
    setcookie('whatever,'true', time() + (900*1100));
    }
    ?>
    PHP:
     
    Meth_, Dec 20, 2007 IP
    Gawk and chriswick like this.
  3. chriswick

    chriswick Peon

    Messages:
    907
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #3


    Thank you! + Rep! :D
     
    chriswick, Dec 20, 2007 IP
  4. Meth_

    Meth_ Well-Known Member

    Messages:
    1,063
    Likes Received:
    72
    Best Answers:
    0
    Trophy Points:
    140
    #4
    oh wait, I forgot "true" is reserved


    <?php
    
    //if the cookie isn't set, 
    if($_COOKIE['whatever']!=1){
    
    //make a file named "myfile.php", and put it in the same directory as this scripts in
    //there's other ways to do this that may be better depending on your situation
    //but this is the easiest and will work for you
    
    require_once('thefile.php');
    
    	}else{
    
    //set the cookie
    setcookie('whatever,'1', time() + (900*1100));
    
    }
    ?> 
    PHP:
     
    Meth_, Dec 20, 2007 IP
  5. tonybogs

    tonybogs Peon

    Messages:
    462
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Whooops, this will flag a warning message if the variable is not set.

    Try:

    if (!empty($_COOKIE['whatever']))
     
    tonybogs, Dec 20, 2007 IP