Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 5th, 2004, 07:06 PM
Registered User
 
Join Date: Apr 2004
Location: Grand, MI, USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Set Cookie

<?php
function showform () {
?>
<form action="index.php" TARGET="_top" method="post">
<table border = "0">
<tr>
<td colspan = "2"><input type="text" name="id" size="13"></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Login"></td>
<td><a href="register.php">Register</a></td>
</tr>
</table>
</form>

<?php
}

if ( ! $_POST['submit'] )
   showform();
else {
mysql_connect("localhost", "rowlandk", "rowlandk8411") or die("Could not connect to database.");
mysql_select_db("rowlandk") or die("Could not use database");
$query = "select id from referee where id=" . $_POST['id'];
$result = mysql_query($query) or die("Error in the query: $query");
 if (mysql_num_rows($result) == 0) {
   echo("Invalid Login");
   showform();
 }
 else {
   echo("Logged In");
   if ( ! $_POST['submitted'] ) {
     login(); ?>
     <form action="index.php" TARGET="_top" method="post">
     <input type="submit" name="submitted" value="Logout">
     </form>
   <?php
   }
   else
     logout();
 }
} ?>

<?php
function login () {
  setcookie("ttslogin", $_POST['id'], time() + 3600);
}
?>
<?php
function logout () {
  setcookie("ttslogin", "", time() - 3600);
  showform();
}
?>


for some reason the second setcookie, is not working, I tried using it on its own page and it seems to work, and I also tried removing all the echos from this page with no luck.

Please Help!!!!
Thanks
Kurt

 
Old April 5th, 2004, 07:08 PM
Friend of Wrox
 
Join Date: Nov 2003
Location: , , .
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I use sessions personally. They're a lot easier.

http://php.net/session

----------
---Snib---
----------

<><
 
Old April 5th, 2004, 07:44 PM
richard.york's Avatar
Wrox Author
Points: 5,506, Level: 31
Points: 5,506, Level: 31 Points: 5,506, Level: 31 Points: 5,506, Level: 31
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Camby, IN, USA.
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Didn't look very closely at your code. Mostly because its horribly unorganized and difficult to read.

Cookies are included in HTTP headers, therefore, setcookie may not be called after output has been sent from the script to the browser (as in echo, print or whatever else). Either call setcookie before output from the script or use output control functions to buffer script output.

RTFM!
http://www.php.net/setcookie
http://www.php.net/outcontrol

If this does nothing to correct the problem, make sure cookies are enabled in your browser (they're turned off by default in MSIE 6).

: )
Rich

::::::::::::::::::::::::::::::::::::::::::
The Spicy Peanut Project
http://www.spicypeanut.net
::::::::::::::::::::::::::::::::::::::::::
 
Old April 6th, 2004, 12:06 PM
Registered User
 
Join Date: Apr 2004
Location: Grand, MI, USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yeah I have to appologize for the mess of code that is. I am a bit pressed for time and I am just trying to get the thing working.

Thanks for the help guys.

 
Old April 6th, 2004, 12:16 PM
Registered User
 
Join Date: Apr 2004
Location: Grand, MI, USA.
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Still not quite sure why it wont work though, Even if I take all of the Echo statements out of my code, the second setcookie, does nothing?

 
Old April 6th, 2004, 01:18 PM
Friend of Wrox
Points: 2,570, Level: 21
Points: 2,570, Level: 21 Points: 2,570, Level: 21 Points: 2,570, Level: 21
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: San Diego, CA, USA
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Matter of style -- you should define all your functions BEFORE they're used. Your code seems to define the functions AFTER they're called.

You have whitespace outside of your PHP tags. I suggest moving your function definitions to the top of the page and removing all whitespace between code.


On another note, if you're too pressed for time to format your code so that it's readable by us, we're probably going to be too pressed for time to format your code so we can read it.

Best of luck.

Take care,

Nik
http://www.bigaction.org/




Similar Threads
Thread Thread Starter Forum Replies Last Post
set the cookie file name crmpicco Javascript How-To 4 February 17th, 2006 05:04 AM
Set Cookie Chapter 8 ? phirun BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 13 May 27th, 2005 11:19 AM
Problem with set cookie chapter 8 stephen_c_ BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 2 December 15th, 2004 02:35 PM
Use cookie to set main frame npepin Javascript 0 October 18th, 2004 02:01 PM
getting set cookie value from a script abbylee26 Javascript 1 July 17th, 2003 03:01 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.