Setting cookies to all pages in your server

Discussion in 'PHP' started by Handsofmodder, Aug 20, 2008.

  1. #1
    I want to create a cookie which can be used for all pages on my website. But, I'm having problems figuring out how to code this. Here's my current code...


    <?php
    setcookie('user',$_POST['my_name'],time()+60*60,'/');
    $user=$_COOKIE['user'];
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Login</title>
    </head>
    <div id="Login" align="left">
    <?php
    if($user==''){
    print '<a href="login.php" target="_self" >Login</a>';
    }else{
    print "Welcome ".$user;
    }
    ?>
    </div>
    <div id="nav" align="right">
    <a href="index.php" target="_self" >Home</a>|
    <a href="register.php" target="_self" >Start A New Account</a>|
    <a href="post.php" target="_self" >Post A New Article</a>|
    </div>
    <br/>
    <br/>
    <body>
    <?php
    if($_POST['_submit_check']) {
    if($form_errors=validate_form()){
    show_form($form_errors);
    } else{
    process_form();
    }
    }else {
    show_form();
    }
    
    function process_form() {
    print "You sucessfully login into the server<br/>";
    print "Welcome back $_POST[my_name]";
    }
    
    function show_form($errors=''){
    if($errors){
    print 'Please correct these errors:<ul><li>';
    print implode('</li><li>',$errors);
    print '</li></ul>';
    }
    
    print<<<_HTML_
    <form method="POST" action="$_SERVER[PHP_SELF]">
    Your Name: <input type="text" name="my_name">
    <br/>
    <br/>
    Your Password:<input type="password" name="my_password">
    <br/>
    <br/>
    Submit:<input type="submit" value="Login">
    <input type="hidden" name="_submit_check" value="1">
    </form>
    _HTML_;
    }
    
    function validate_form(){
    $Conn=mysql_connect('localhost', 'myhost', 'mypassword');
    $db=mysql_select_db('mydatabase', $Conn);
    $query = "SELECT * FROM users WHERE user_name='$_POST[my_name]'"; 	 
    $result = mysql_query($query) or die(mysql_error());
    $row = mysql_fetch_array($result) or die(mysql_error());
    if (strlen($_POST['my_name']) < 3){
    $errors[]= 'Your Username Must Be More Than 3 Characters';
    }
    if (strlen($_POST['my_password']) < 3){
    $errors[]= 'Your Password Must Be More Than 3 Characters';
    }
    if ($_POST['my_name']!=$row['user_name']){
    $errors[]= 'Invalid User Name';
    }
    if ($_POST['my_password']!=$row['user_password']){
    $errors[]= 'Invalid Password';
    }
    return $errors;
    }
    ?>
    </body>
    </html>
    
    PHP:
     
    Handsofmodder, Aug 20, 2008 IP
  2. jack_ss

    jack_ss Guest

    Messages:
    94
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    You might be/are reseting your cookie each time you load the page. Try this:
    <?
    if (!$_COOKIE[user] && $_POST[my_name]) {
        setcookie('user', $_POST[my_name], time()+3600);
        $user=$_COOKIE['user'];
    }
    ?>
    PHP:
     
    jack_ss, Aug 20, 2008 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    You can't get the cookie right after setting it. You have to reload the page, to have the browser send the cookie back to the script. THEN it'll be in the $_COOKIE array.

    
    // Sets the cookie:
    setcookie('user',$_POST['my_name'],time()+60*60,'/');
    // $_COOKIE['user'] will be blank until the page is reloaded.
    $user=$_COOKIE['user'];
    
    PHP:
    ... Do it this way:
    
    $user = isset($_POST['my_name']) ? $_POST['my_name'] : $_COOKIE['name'];
    
    PHP:
    And don't be lazy and quote your array keys.... (@ jack_ss)
     
    nico_swd, Aug 21, 2008 IP
  4. jack_ss

    jack_ss Guest

    Messages:
    94
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Heh heh. That's why I leave coding to the coders. I'm creative which makes me a hack of a script kiddie. Everything I make, works. But, apparently isn't always right. I'm learning.
     
    jack_ss, Aug 21, 2008 IP
  5. William[ws]

    William[ws] Peon

    Messages:
    47
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    LOL funny statement made!!....

    did u get it to work my man?
     
    William[ws], Aug 22, 2008 IP