trying to write a cookie
-
Subject is pretty self explanatory. This is what I have for code and then I'll try to explain it.
<?php
echo "Login page";
?>
<form action="rememberMe.php" method="post">
<div style="width: 30em;">
<label for="name">Please Enter Your Name:</label>
<input type="text" name="name" id="name" value="" />
<br />
<label for="location">In what location do you work<br />(Portland/Brewer)?</label>
<input type="text" name="location" id="location value="" />
<div style="clear: both;">
<input type="submit" name="sendInfo" value="sendInfo" />
</div>
</div>
</form>and then
<?php
$name = $_POST["name"];
$location = $_POST["location"];
$cookie = $name. "," . $location;
//echo $cookie;setcookie( "thomasEmployee", $cookie, time() + 60 * 60 * 24 * 365), "/", ".linuxserver", false, true);
if (isset($_COOKIE['thomasEmployee'])) {
var_dump($_COOKIE);
}
else {
header( 'Location: index.html' ) ;
}?>
This is the page where I'm trying to "write" the cookie. The name I'm trying to use for the cookie is thomasEmployee, using the variable I created ($cookie) to hold the value, setting it for a year (though I'm sure it'll get deleted before then), and the domain (part I'm not sure about) is linuxserver. This is for a intranet site, the url for the indexpage is typed linuxserver/index.html so I'm guessing the domain is linuxserver? What I have right now doesn't seem to be writing anything as when I go to look for it, it's not there and when I try to get to my test page, it brings me right back to the login page.
<?php
if (isset($_COOKIE["thomasEmployee"])) {
// var_dump($_COOKIE);
}
else {
header( 'Location: login.php' ) ;
}?>
<html>
<head>That's the top of the page where I currently have it checking for the cookie. I'm sure it's something stupid, so thanks in advance!!!
-
Subject is pretty self explanatory. This is what I have for code and then I'll try to explain it.
<?php
echo "Login page";
?>
<form action="rememberMe.php" method="post">
<div style="width: 30em;">
<label for="name">Please Enter Your Name:</label>
<input type="text" name="name" id="name" value="" />
<br />
<label for="location">In what location do you work<br />(Portland/Brewer)?</label>
<input type="text" name="location" id="location value="" />
<div style="clear: both;">
<input type="submit" name="sendInfo" value="sendInfo" />
</div>
</div>
</form>and then
<?php
$name = $_POST["name"];
$location = $_POST["location"];
$cookie = $name. "," . $location;
//echo $cookie;setcookie( "thomasEmployee", $cookie, time() + 60 * 60 * 24 * 365), "/", ".linuxserver", false, true);
if (isset($_COOKIE['thomasEmployee'])) {
var_dump($_COOKIE);
}
else {
header( 'Location: index.html' ) ;
}?>
This is the page where I'm trying to "write" the cookie. The name I'm trying to use for the cookie is thomasEmployee, using the variable I created ($cookie) to hold the value, setting it for a year (though I'm sure it'll get deleted before then), and the domain (part I'm not sure about) is linuxserver. This is for a intranet site, the url for the indexpage is typed linuxserver/index.html so I'm guessing the domain is linuxserver? What I have right now doesn't seem to be writing anything as when I go to look for it, it's not there and when I try to get to my test page, it brings me right back to the login page.
<?php
if (isset($_COOKIE["thomasEmployee"])) {
// var_dump($_COOKIE);
}
else {
header( 'Location: login.php' ) ;
}?>
<html>
<head>That's the top of the page where I currently have it checking for the cookie. I'm sure it's something stupid, so thanks in advance!!!
First guess is that you've run into the old "too late to send headers" problem. Quoting from the PHP manual:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
Note the last few words. A single space or newline can break your code. HTH PeterSoftware rusts. Simon Stephenson, ca 1994.
-
First guess is that you've run into the old "too late to send headers" problem. Quoting from the PHP manual:
setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.
Note the last few words. A single space or newline can break your code. HTH PeterSoftware rusts. Simon Stephenson, ca 1994.
ok I have changed the code to this:
<?php
$name = $_POST["name"];
$location = $_POST["location"];
$cookie = $name. "," . $location;
setcookie("thomasEmployee",$cookie,time() + 60 * 60 * 24 * 365,"/","linuxserver",false,true);
header('Location: http://linuxserver/index.html');
?>I have also tried:
setcookie("thomasEmployee",$cookie,time() + 31536000,"/","linuxserver",false,true);
and
setcookie("thomasEmployee",$cookie,time()+31536000,"/","linuxserver",false,true);
but only this seems to work:
setcookie("thomasEmployee",$cookie,time()+31536000);
granted this is a intranet site so not a huge deal, but it would be nice to know why the rest isn't working. Also this will be available to all pages from this site correct? I'm pretty sure the answer is yes, but I'd like to be sure.
-
ok I have changed the code to this:
<?php
$name = $_POST["name"];
$location = $_POST["location"];
$cookie = $name. "," . $location;
setcookie("thomasEmployee",$cookie,time() + 60 * 60 * 24 * 365,"/","linuxserver",false,true);
header('Location: http://linuxserver/index.html');
?>I have also tried:
setcookie("thomasEmployee",$cookie,time() + 31536000,"/","linuxserver",false,true);
and
setcookie("thomasEmployee",$cookie,time()+31536000,"/","linuxserver",false,true);
but only this seems to work:
setcookie("thomasEmployee",$cookie,time()+31536000);
granted this is a intranet site so not a huge deal, but it would be nice to know why the rest isn't working. Also this will be available to all pages from this site correct? I'm pretty sure the answer is yes, but I'd like to be sure.
MacRaider4 wrote:
it would be nice to know why the rest isn't working.
All I can suggest is adding the remaining optional parameters one at a time and see what happens with, say, Firebug, and a peek into the server logs.
MacRaider4 wrote:
Also this will be available to all pages from this site correct? I'm pretty sure the answer is yes, but I'd like to be sure.
I'm pretty sure too. Suck it and see, in with the testing I suggested above. Cheers, Peter
Software rusts. Simon Stephenson, ca 1994.
-
Subject is pretty self explanatory. This is what I have for code and then I'll try to explain it.
<?php
echo "Login page";
?>
<form action="rememberMe.php" method="post">
<div style="width: 30em;">
<label for="name">Please Enter Your Name:</label>
<input type="text" name="name" id="name" value="" />
<br />
<label for="location">In what location do you work<br />(Portland/Brewer)?</label>
<input type="text" name="location" id="location value="" />
<div style="clear: both;">
<input type="submit" name="sendInfo" value="sendInfo" />
</div>
</div>
</form>and then
<?php
$name = $_POST["name"];
$location = $_POST["location"];
$cookie = $name. "," . $location;
//echo $cookie;setcookie( "thomasEmployee", $cookie, time() + 60 * 60 * 24 * 365), "/", ".linuxserver", false, true);
if (isset($_COOKIE['thomasEmployee'])) {
var_dump($_COOKIE);
}
else {
header( 'Location: index.html' ) ;
}?>
This is the page where I'm trying to "write" the cookie. The name I'm trying to use for the cookie is thomasEmployee, using the variable I created ($cookie) to hold the value, setting it for a year (though I'm sure it'll get deleted before then), and the domain (part I'm not sure about) is linuxserver. This is for a intranet site, the url for the indexpage is typed linuxserver/index.html so I'm guessing the domain is linuxserver? What I have right now doesn't seem to be writing anything as when I go to look for it, it's not there and when I try to get to my test page, it brings me right back to the login page.
<?php
if (isset($_COOKIE["thomasEmployee"])) {
// var_dump($_COOKIE);
}
else {
header( 'Location: login.php' ) ;
}?>
<html>
<head>That's the top of the page where I currently have it checking for the cookie. I'm sure it's something stupid, so thanks in advance!!!
We use the following to store the users ID when they logon... setcookie("UID", $_REQUEST['cuser'], time()+2592000, "/"); ...this allows us to remember a users ID, BUT it is only available after the page is reloaded. So after the above line you could add... header("Location: $http_url"); You could then check to see if it has been set. ...or you could push the value into the Session as well... $_SESSION['UID'] = $_REQUEST['cuser']; Hope this helps.
-
ok I have changed the code to this:
<?php
$name = $_POST["name"];
$location = $_POST["location"];
$cookie = $name. "," . $location;
setcookie("thomasEmployee",$cookie,time() + 60 * 60 * 24 * 365,"/","linuxserver",false,true);
header('Location: http://linuxserver/index.html');
?>I have also tried:
setcookie("thomasEmployee",$cookie,time() + 31536000,"/","linuxserver",false,true);
and
setcookie("thomasEmployee",$cookie,time()+31536000,"/","linuxserver",false,true);
but only this seems to work:
setcookie("thomasEmployee",$cookie,time()+31536000);
granted this is a intranet site so not a huge deal, but it would be nice to know why the rest isn't working. Also this will be available to all pages from this site correct? I'm pretty sure the answer is yes, but I'd like to be sure.
-
ok I have changed the code to this:
<?php
$name = $_POST["name"];
$location = $_POST["location"];
$cookie = $name. "," . $location;
setcookie("thomasEmployee",$cookie,time() + 60 * 60 * 24 * 365,"/","linuxserver",false,true);
header('Location: http://linuxserver/index.html');
?>I have also tried:
setcookie("thomasEmployee",$cookie,time() + 31536000,"/","linuxserver",false,true);
and
setcookie("thomasEmployee",$cookie,time()+31536000,"/","linuxserver",false,true);
but only this seems to work:
setcookie("thomasEmployee",$cookie,time()+31536000);
granted this is a intranet site so not a huge deal, but it would be nice to know why the rest isn't working. Also this will be available to all pages from this site correct? I'm pretty sure the answer is yes, but I'd like to be sure.
three suggestions: 1. read the documentation (it may be there), and use Google (others may have encountered the same problem). 2. the function has a return value; always check the return value when there is one, it is there for a reason. 3. the domain parameter seems to expect a domain name (or a partial one, starting with a period), not a host name. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
-
three suggestions: 1. read the documentation (it may be there), and use Google (others may have encountered the same problem). 2. the function has a return value; always check the return value when there is one, it is there for a reason. 3. the domain parameter seems to expect a domain name (or a partial one, starting with a period), not a host name. :)
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
Not quite a month since I've worked on this (this is a side project). Anyways, I think I'll have some time this week to get back to PHP and the web. I'll go through what you guys wrote and if I have any further questions or get it "working" I'll let you know. Thanks again!!