472,353 Members | 1,368 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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?
May 21 '09 #1
9 26005
prabirchoudhury
162 100+
hey ..
you make sure doing the right code for the sercookie

Expand|Select|Wrap|Line Numbers
  1. examples: 
  2.  
  3.  
  4. setcookie("mycookie", $test, time() + 3600);
  5. setcookie("mycookie","",time() - 3600);
  6.  
  7. setcookie('my_id', $_REQUEST['my_id'], time() + 60*60*24*365);
  8.  
  9.  
  10.  
  11.  
if still problem there plz mention..
May 22 '09 #2
Atli
5,058 Expert 4TB
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.
May 22 '09 #3
I have tried all examples mentioned by "prabirchoudhury" 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?
May 23 '09 #4
Markus
6,050 Expert 4TB
Are you sure that you have cookies enabled in your browser?
May 23 '09 #5
Atli
5,058 Expert 4TB
Try this:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     error_reporting(E_ALL);
  3.     ini_set("display_errors", true);
  4.     header("Content-Type: text/plain");
  5.  
  6.     if(isset($_COOKIE['cookietest'])) {
  7.         var_dump($_COOKIE);
  8.     }
  9.     else {
  10.         $time = time();
  11.         setcookie("cookietest", $time, $time + 3600);
  12.         echo "Cookie set at: " . $time;
  13.     }
  14. ?>
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.
May 24 '09 #6
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.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.     $result = mysql_connect("*********", "******", "*********");
  3.     $result = mysql_select_db("test");
  4.     $user = $_POST["usr"];
  5.     $pass = $_POST["pss"];
  6.     $usr_stat = 0;
  7.     $pss_stat = 0;
  8.     $sql = "SELECT password FROM ".tbl_login." WHERE login = '$user';";
  9.     $result = mysql_query($sql);
  10.     $row_array = mysql_fetch_array($result);
  11.     $expire = time()+60*60*24*30;
  12.  
  13.     if(!$result || (mysql_numrows($result) < 1)){
  14.       $usr_stat = 0;
  15.     }
  16.     else{
  17.       $usr_stat = 1;
  18.     }
  19.     if($pass == $row_array['password']){
  20.       $pss_stat = 1;
  21.     }
  22.     else{
  23.       $pss_stat = 0;
  24.     }
  25.     if($usr_stat == 1 && $pss_stat == 1){
  26.       setcookie ('curruser', $_POST['usr'], $expire);
  27.       header('Location: cktest2.php');
  28.     }
  29. ?>
  30. <html>
  31. <head>
  32. <title>Studying PHP</title>
  33. <link rel="stylesheet" href="css/main.css">
  34. </head>
  35. <body>
  36.  
  37. <h1>Login test</h1>
  38.  
  39. <form name="login" action="login.php" method="POST">
  40.   Username: <input type="text" value="" name="usr">
  41.   Password: <input type="password" value="" name="pss">
  42.   <input type="submit" value="Submit">
  43. </form>
  44.  
  45. <?php
  46.   if(!$result || (mysql_numrows($result) < 1)){
  47.     echo "<p>Username invalid</p>";
  48.   }
  49.   else{
  50.     echo "<p>Username valid</p>";
  51.   }
  52.   if($pass == $row_array['password']){
  53.     echo "<p>Password valid</p>";
  54.   }
  55.   else{
  56.     echo "<p>Password invalid</p>";
  57.   }
  58.   if($usr_stat == 1 && $pss_stat == 1){
  59.     echo "<p>Login details valid</p>";
  60.   }
  61.   else{
  62.     echo "<p>Login details invalid</p>";
  63.   }
  64.   if(isset($_COOKIE['curruser'])){
  65.     echo "<p>Welcome ".$_COOKIE['curruser'].", you are now logged in using cookies.</p>";
  66.   }
  67.   else{
  68.     echo "<p>You are not logged in.</p>";
  69.   }
  70.   print_r($_COOKIE);
  71. ?>
  72.  
  73. </body>
  74. </html>
  75.  
May 24 '09 #7
Atli
5,058 Expert 4TB
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:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. if(isset($_POST["usr"])) {
  3.     //$result = mysql_connect("*********", "******", "*********");
  4.     //$result = mysql_select_db("test");
  5.     $user = $_POST["usr"];
  6.     $pass = $_POST["pss"];
  7.     $usr_stat = 0;
  8.     $pss_stat = 0;
  9.     //$sql = "SELECT password FROM ".tbl_login." WHERE login = '$user';";
  10.     //$result = mysql_query($sql);
  11.     //$row_array = mysql_fetch_array($result);
  12.     $expire = time()+60*60*24*30;
  13.  
  14.     if(false/*!$result || (mysql_numrows($result) < 1)*/){
  15.         $usr_stat = 0;
  16.     }
  17.     else{
  18.         $usr_stat = 1;
  19.     }
  20.     if(true/*$pass == $row_array['password']*/){
  21.         $pss_stat = 1;
  22.     }
  23.     else{
  24.         $pss_stat = 0;
  25.     }
  26.     if($usr_stat == 1 && $pss_stat == 1){
  27.         setcookie ('curruser', $_POST['usr'], $expire);
  28.         header('Location: test.php');
  29.     }
  30. }
  31. ?>
  32. <html>
  33.     <head>
  34.         <title>Studying PHP</title>
  35.         <link rel="stylesheet" href="css/main.css">
  36.     </head>
  37.     <body>
  38.  
  39.         <h1>Login test</h1>
  40.  
  41.         <form name="login" action="test.php" method="POST">
  42.             Username: <input type="text" value="" name="usr">
  43.             Password: <input type="password" value="" name="pss">
  44.             <input type="submit" value="Submit">
  45.         </form>
  46.         <pre>
  47. <?php
  48. if(isset($_POST["usr"])) {
  49.     if($usr_stat == 0){
  50.         echo "<p>Username invalid</p>";
  51.     }
  52.     else{
  53.         echo "<p>Username valid</p>";
  54.     }
  55.     if($pss_stat == 0){
  56.         echo "<p>Password valid</p>";
  57.     }
  58.     else{
  59.         echo "<p>Password invalid</p>";
  60.     }
  61.     if($usr_stat == 1 && $pss_stat == 1){
  62.         echo "<p>Login details valid</p>";
  63.     }
  64.     else{
  65.         echo "<p>Login details invalid</p>";
  66.     }
  67. }
  68. if(isset($_COOKIE['curruser'])){
  69.     echo "<p>Welcome ".$_COOKIE['curruser'].", you are now logged in using cookies.</p>";
  70. }
  71. else{
  72.     echo "<p>You are not logged in.</p>";
  73. }
  74.  
  75. print_r($_COOKIE);
  76. ?>
  77.         </pre>
  78.     </body>
  79. </html>
May 26 '09 #8
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.
May 26 '09 #9
Atli
5,058 Expert 4TB
Glad you got it working.

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

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Craig Matthews | last post by:
hello, any idea why the following code does not work?!! <? $cookieExpire = 864000000; setcookie("a", "love", $cookieExpire);...
16
by: Phil Powell | last post by:
Fourth attempt.. it fails now in login, I check by printing $_COOKIE and there is no value there! Guys, what on earth do I do about this???? ...
1
by: TG | last post by:
What is wrong with the following code? I have set allow all cookies on for my browser. Why does this always redirect to NoCookies.htm? Please see...
5
by: Ben | last post by:
Hi all, In my .php file, I'm using both session_start() and setcookie() before <html> tag. It gives me following warning message: Warning:...
2
by: Katherine Hall | last post by:
I am trying to set a cookie and use it later, but as soon as put a redirect or anything, I loose it. So when I first print it out, it exists....
1
by: Rewire | last post by:
This script seems to work fine with firefox but not with IE. It just sets style="display:none" or style="display:" to show / hide submenu items on my...
9
by: LayneMitch via WebmasterKB.com | last post by:
Hello. Got another one for you folks. I'm working on this problem that wants me to 1. Prompt for name 2. Use pop-up box with name 3. Display...
8
by: SupraFast | last post by:
I have two hosting accounts. On one, my setcookie script works fine; cookies are created. On the other, the same script doesn't work. The function...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.