setcookie() not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • wangers16
    New Member
    • Jul 2007
    • 57

    setcookie() not working

    Hi,

    I am quite new to PHP and recently I have been attempting to create a login script, just one problem, the setcookie function isn't working.

    I have tried a basic php file with nothing other than a function to set and retrieve the cookie but still nothing.

    Why is this happening?
  • prabirchoudhury
    New Member
    • May 2009
    • 162

    #2
    setcookie() not working

    hey ..
    you make sure doing the right code for the sercookie

    Code:
    examples: 
    
    
    setcookie("mycookie", $test, time() + 3600);
    setcookie("mycookie","",time() - 3600);
    
    setcookie('my_id', $_REQUEST['my_id'], time() + 60*60*24*365);
    if still problem there plz mention..

    Comment

    • Atli
      Recognized Expert Expert
      • Nov 2006
      • 5062

      #3
      Hi.

      There are a lot of reasons why thins could be happening, and simply stating that the function isn't working doesn't really narrow the list down.

      We need a lot more details to be able to help with this.
      Code examples and error messages are always helpful to.

      Comment

      • wangers16
        New Member
        • Jul 2007
        • 57

        #4
        I have tried all examples mentioned by "prabirchoudhur y" and none seem to work, everytime I try and retrieve the cookie, the page either comes up blank or displays Array ( ), depending upon the method I use to view them.

        Unfortunately there are no error messages to provide an insight as to why this is happening.

        P.S. I am using my own server which again I have only just begun to do, is there anything that I need to set in the PHP config file in order to get cookies to work?

        Comment

        • Markus
          Recognized Expert Expert
          • Jun 2007
          • 6092

          #5
          Are you sure that you have cookies enabled in your browser?

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Try this:
            [code=php]<?php
            error_reporting (E_ALL);
            ini_set("displa y_errors", true);
            header("Content-Type: text/plain");

            if(isset($_COOK IE['cookietest'])) {
            var_dump($_COOK IE);
            }
            else {
            $time = time();
            setcookie("cook ietest", $time, $time + 3600);
            echo "Cookie set at: " . $time;
            }
            ?>[/code]
            When you first run it, it sets the cookie. Every request after that should show that cookie.

            You shouldn't need to configure PHP in any way for this to work.
            (I'm not even sure you can configure it to not support cookies :P)

            If this doesn't work (if it keeps setting the cookie, rather than displaying it), then I'm guessing your browser doesn't support cookies.

            Try using Firefox with the Web Developer plugin. It can show you all the cookies that are set on your browser, and if they are incorrectly set in some way.

            Comment

            • wangers16
              New Member
              • Jul 2007
              • 57

              #7
              I used the code you gave me and it seems to work, so I am now wondering, is the setcookie function the problem.

              Here is my login script that I have tried to make, all of it works except for setting the cookie in any browser.

              Code:
              <?php
              	$result = mysql_connect("*********", "******", "*********");
              	$result = mysql_select_db("test");
              	$user = $_POST["usr"];
              	$pass = $_POST["pss"];
              	$usr_stat = 0;
              	$pss_stat = 0;
              	$sql = "SELECT password FROM ".tbl_login." WHERE login = '$user';";
              	$result = mysql_query($sql);
              	$row_array = mysql_fetch_array($result);
              	$expire = time()+60*60*24*30;
              	
              	if(!$result || (mysql_numrows($result) < 1)){
              	  $usr_stat = 0;
              	}
              	else{
              	  $usr_stat = 1;
              	}
              	if($pass == $row_array['password']){
              	  $pss_stat = 1;
              	}
              	else{
              	  $pss_stat = 0;
              	}
              	if($usr_stat == 1 && $pss_stat == 1){
              	  setcookie ('curruser', $_POST['usr'], $expire);
              	  header('Location: cktest2.php');
              	}
              ?>
              <html>
              <head>
              <title>Studying PHP</title>
              <link rel="stylesheet" href="css/main.css">
              </head>
              <body>
              
              <h1>Login test</h1>
              
              <form name="login" action="login.php" method="POST">
                Username: <input type="text" value="" name="usr">
                Password: <input type="password" value="" name="pss">
                <input type="submit" value="Submit">
              </form>
              
              <?php
                if(!$result || (mysql_numrows($result) < 1)){
                  echo "<p>Username invalid</p>";
                }
                else{
                  echo "<p>Username valid</p>";
                }
                if($pass == $row_array['password']){
                  echo "<p>Password valid</p>";
                }
                else{
                  echo "<p>Password invalid</p>";
                }
                if($usr_stat == 1 && $pss_stat == 1){
                  echo "<p>Login details valid</p>";
                }
                else{
                  echo "<p>Login details invalid</p>";
                }
                if(isset($_COOKIE['curruser'])){
                  echo "<p>Welcome ".$_COOKIE['curruser'].", you are now logged in using cookies.</p>";
                }
                else{
                  echo "<p>You are not logged in.</p>";
                }
                print_r($_COOKIE);
              ?>
              
              </body>
              </html>

              Comment

              • Atli
                Recognized Expert Expert
                • Nov 2006
                • 5062

                #8
                In the SQL on line #8 you use a constant 'tbl_login', but you don't set it anywhere.
                Where does it come from?

                Is this script being included into another script, or anything like that?

                Keep in mind that the setcookie function doesn't work if you send anything to the output buffer before it is used.
                Even a white-space before the <?php tag will cause the function to fail.

                I tried your script with minor modifications to switch your DB validation to a static validation, and it worked fine.
                This is the modified code I used:
                [code=php]<?php
                if(isset($_POST["usr"])) {
                //$result = mysql_connect(" *********", "******", "*********" );
                //$result = mysql_select_db ("test");
                $user = $_POST["usr"];
                $pass = $_POST["pss"];
                $usr_stat = 0;
                $pss_stat = 0;
                //$sql = "SELECT password FROM ".tbl_login ." WHERE login = '$user';";
                //$result = mysql_query($sq l);
                //$row_array = mysql_fetch_arr ay($result);
                $expire = time()+60*60*24 *30;

                if(false/*!$result || (mysql_numrows( $result) < 1)*/){
                $usr_stat = 0;
                }
                else{
                $usr_stat = 1;
                }
                if(true/*$pass == $row_array['password']*/){
                $pss_stat = 1;
                }
                else{
                $pss_stat = 0;
                }
                if($usr_stat == 1 && $pss_stat == 1){
                setcookie ('curruser', $_POST['usr'], $expire);
                header('Locatio n: test.php');
                }
                }
                ?>
                <html>
                <head>
                <title>Studyi ng PHP</title>
                <link rel="stylesheet " href="css/main.css">
                </head>
                <body>

                <h1>Login test</h1>

                <form name="login" action="test.ph p" method="POST">
                Username: <input type="text" value="" name="usr">
                Password: <input type="password" value="" name="pss">
                <input type="submit" value="Submit">
                </form>
                <pre>
                <?php
                if(isset($_POST["usr"])) {
                if($usr_stat == 0){
                echo "<p>Usernam e invalid</p>";
                }
                else{
                echo "<p>Usernam e valid</p>";
                }
                if($pss_stat == 0){
                echo "<p>Passwor d valid</p>";
                }
                else{
                echo "<p>Passwor d invalid</p>";
                }
                if($usr_stat == 1 && $pss_stat == 1){
                echo "<p>Login details valid</p>";
                }
                else{
                echo "<p>Login details invalid</p>";
                }
                }
                if(isset($_COOK IE['curruser'])){
                echo "<p>Welcome ".$_COOKIE['curruser'].", you are now logged in using cookies.</p>";
                }
                else{
                echo "<p>You are not logged in.</p>";
                }

                print_r($_COOKI E);
                ?>
                </pre>
                </body>
                </html>[/code]

                Comment

                • wangers16
                  New Member
                  • Jul 2007
                  • 57

                  #9
                  Ahh, it's now working :)

                  I now know what the problem was thanks to Atli, I was using the constant where I shouldn't have, such simple things.

                  Sorry about that, i'll try and remember not to do that again, silly mistake.

                  Thanks for all your help.

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    Glad you got it working.

                    Those little things, like spelling errors and such, always seem to cause the most trouble, don't they :)

                    Comment

                    Working...