Pi_Mastuh Posted January 18, 2007 Share Posted January 18, 2007 I'm trying to make cookies that will keep a user logged in after they close the page but they're not working. Can someone help?Home page (index.php):[code]<html><head><meta http-equiv="Content-Language" content="en-us"><TITLE>Welcome to Net-Petz! ~ Home</TITLE><META NAME="Description" CONTENT="Net-Petz is the newest virtual pet site on the internet. It has fun games, lots of cool petz to adopt, and tons of awesome items! It's completely free!"><META NAME="Keywords" CONTENT="petz net virtual pets pet fun games, game, "><script language="JavaScript" fptype="dynamicanimation"><!--function dynAnimation() {}function clickSwapImg() {}//--></script><script language="JavaScript1.2" fptype="dynamicanimation" src="animate.js"></script><style type="text/css"><!--.style3 {font-family: Arial, Helvetica, sans-serif}--></style><? if(isset($_COOKIE['preuserID']) && !empty($_COOKIE[';preuserID']) and isset($_COOKIE['preuserPassword']) && !empty($_COOKIE[';preuserPassword'])){ // your cookie is found $preuserID = htmlspecialchars($_COOKIE['preuserID'], ENT_QUOTES); $preuserPassword = htmlspecialchars($_COOKIE['preuserPassword'], ENT_QUOTES); header("Location:/reg/home.php"); exit();}?></head><body onLoad="dynAnimation()"><div align="center"> <table border="1" cellspacing="1" bordercolor="#FFFFFF" width="1%" id="AutoNumber1" height="838" align="left"> <tr> <td width="100%" height="90" colspan="2" align="center" valign="top" bordercolor="#FFFFFF" bordercolorlight="#FFFFFF" bordercolordark="#FFFFFF"> <p> <img border="0" src="all_n.jpg" width="1024" height="91"></td> </tr> <tr> <td width="12%" height="747" align="center" valign="top" bordercolor="#FFFFFF"> <p> <img border="0" src="titleexplore.jpg" width="130" height="22"><br> <a onMouseOver="document['fpAnimswapImgFP21'].imgRolln=document['fpAnimswapImgFP21'].src;document['fpAnimswapImgFP21'].src=document['fpAnimswapImgFP21'].lowsrc;" onMouseOut="document['fpAnimswapImgFP21'].src=document['fpAnimswapImgFP21'].imgRolln" href="oops.php"> <img border="0" src="world1.jpg" id="fpAnimswapImgFP21" name="fpAnimswapImgFP21" dynamicanimation="fpAnimswapImgFP21" lowsrc="world2.jpg" width="130" height="22"></a><br> <a onMouseOver="document['fpAnimswapImgFP22'].imgRolln=document['fpAnimswapImgFP22'].src;document['fpAnimswapImgFP22'].src=document['fpAnimswapImgFP22'].lowsrc;" onMouseOut="document['fpAnimswapImgFP22'].src=document['fpAnimswapImgFP22'].imgRolln" href="oops.php"> <img border="0" src="market1.jpg" id="fpAnimswapImgFP22" name="fpAnimswapImgFP22" dynamicanimation="fpAnimswapImgFP22" lowsrc="market2.jpg" width="130" height="22"></a><br> <img border="0" src="titlefun.jpg" width="130" height="22"><br> <a onMouseOver="document['fpAnimswapImgFP23'].imgRolln=document['fpAnimswapImgFP23'].src;document['fpAnimswapImgFP23'].src=document['fpAnimswapImgFP23'].lowsrc;" onMouseOut="document['fpAnimswapImgFP23'].src=document['fpAnimswapImgFP23'].imgRolln" href="oops.php"> <img border="0" src="games1.jpg" id="fpAnimswapImgFP23" name="fpAnimswapImgFP23" dynamicanimation="fpAnimswapImgFP23" lowsrc="games2.jpg" width="130" height="22"></a><br> <a onMouseOver="document['fpAnimswapImgFP24'].imgRolln=document['fpAnimswapImgFP24'].src;document['fpAnimswapImgFP24'].src=document['fpAnimswapImgFP24'].lowsrc;" onMouseOut="document['fpAnimswapImgFP24'].src=document['fpAnimswapImgFP24'].imgRolln" href="oops.php"> <img border="0" src="boards1.jpg" id="fpAnimswapImgFP24" name="fpAnimswapImgFP24" dynamicanimation="fpAnimswapImgFP24" lowsrc="boards2.jpg" width="130" height="22"></a><br> <a onMouseOver="document['fpAnimswapImgFP25'].imgRolln=document['fpAnimswapImgFP25'].src;document['fpAnimswapImgFP25'].src=document['fpAnimswapImgFP25'].lowsrc;" onMouseOut="document['fpAnimswapImgFP25'].src=document['fpAnimswapImgFP25'].imgRolln" href="javascript:void(0)"> <img border="0" src="images/Sidebar%20&%20Layout/mail1.jpg" id="fpAnimswapImgFP25" name="fpAnimswapImgFP25" dynamicanimation="fpAnimswapImgFP25" lowsrc="images/Sidebar%20&%20Layout/mail2.jpg" width="130" height="22"></a><br> <a onMouseOver="document['fpAnimswapImgFP26'].imgRolln=document['fpAnimswapImgFP26'].src;document['fpAnimswapImgFP26'].src=document['fpAnimswapImgFP26'].lowsrc;" onMouseOut="document['fpAnimswapImgFP26'].src=document['fpAnimswapImgFP26'].imgRolln" href="stuff.php"> <img border="0" src="stuff1.jpg" id="fpAnimswapImgFP26" name="fpAnimswapImgFP26" dynamicanimation="fpAnimswapImgFP26" lowsrc="stuff2.jpg" width="130" height="22"></a><br> <img border="0" src="titlemisc.jpg" width="130" height="22"><br> <a onMouseOver="document['fpAnimswapImgFP27'].imgRolln=document['fpAnimswapImgFP27'].src;document['fpAnimswapImgFP27'].src=document['fpAnimswapImgFP27'].lowsrc;" onMouseOut="document['fpAnimswapImgFP27'].src=document['fpAnimswapImgFP27'].imgRolln" href="login.php"> <img border="0" src="in1.jpg" id="fpAnimswapImgFP27" name="fpAnimswapImgFP27" dynamicanimation="fpAnimswapImgFP27" lowsrc="in2.jpg" width="130" height="22"></a><br> <a onMouseOver="document['fpAnimswapImgFP28'].imgRolln=document['fpAnimswapImgFP28'].src;document['fpAnimswapImgFP28'].src=document['fpAnimswapImgFP28'].lowsrc;" onMouseOut="document['fpAnimswapImgFP28'].src=document['fpAnimswapImgFP28'].imgRolln" href="logout.php"> <img border="0" src="out1.jpg" id="fpAnimswapImgFP28" name="fpAnimswapImgFP28" dynamicanimation="fpAnimswapImgFP28" lowsrc="out2.jpg" width="130" height="22"></a><br> <a onMouseOver="document['fpAnimswapImgFP29'].imgRolln=document['fpAnimswapImgFP29'].src;document['fpAnimswapImgFP29'].src=document['fpAnimswapImgFP29'].lowsrc;" onMouseOut="document['fpAnimswapImgFP29'].src=document['fpAnimswapImgFP29'].imgRolln" href="help.php"> <img border="0" src="help1.jpg" id="fpAnimswapImgFP29" name="fpAnimswapImgFP29" dynamicanimation="fpAnimswapImgFP29" lowsrc="help2.jpg" width="130" height="22"></a><br> <a onMouseOver="document['fpAnimswapImgFP30'].imgRolln=document['fpAnimswapImgFP30'].src;document['fpAnimswapImgFP30'].src=document['fpAnimswapImgFP30'].lowsrc;" onMouseOut="document['fpAnimswapImgFP30'].src=document['fpAnimswapImgFP30'].imgRolln" href="contact.php"> <img border="0" src="contact1.jpg" id="fpAnimswapImgFP30" name="fpAnimswapImgFP30" dynamicanimation="fpAnimswapImgFP30" lowsrc="contact2.jpg" width="130" height="22"></a></p> <div align="center" style="width: 130; height: 176"> <center> <table border="3" cellspacing="1" bordercolor="#000099" width="100%" id="AutoNumber2" height="253"> <tr> <td width="100%" height="126" align="center" bordercolor="#000099"> <font color="#000099"> <? print "Hi $preuserName!"; ?></font></b><br> <b> <font color="#000099"><? print "$points Net-Pointz."; ?></font></td> </tr> <tr> <td width="100%" height="127" align="center" bordercolor="#000099"> <a style="text-decoration: none" dynamicanimation="fpAnimformatRolloverFP1" fprolloverstyle="color: #FF0000; text-decoration: overline underline; font-weight: bold" onMouseOver="rollIn(this)" onMouseOut="rollOut(this)" language="Javascript1.2" href="oops.php">My Petz</a><br> <a style="text-decoration: none" dynamicanimation="fpAnimformatRolloverFP1" fprolloverstyle="color: #FF0000; text-decoration: overline underline; font-weight: bold" onMouseOver="rollIn(this)" onMouseOut="rollOut(this)" language="Javascript1.2" href="oops.php">My Inventory</a></td> </tr> </table> </center> </div> <p><br> <br> </td> <td width="88%" height="747" align="center" valign="top"> <table border="4" cellspacing="1" width="67%" id="AutoNumber3" bordercolorlight="#000099" bordercolordark="#000099"> <tr> <td width="100%" bordercolorlight="#000099" bordercolordark="#000099"> <img border="0" src="Welcome%20logo.gif" width="618" height="44"></td> </tr> </table> <p>Net-Petz is the newest virtual pet site on the internet. It's filled with cool petz, fun games, exciting places, and much more. It's free to sign up so what are you waiting for? <a style="text-decoration: none" dynamicanimation="fpAnimformatRolloverFP1" fprolloverstyle="color: #FF0000; text-decoration: overline underline; font-weight: bold" onMouseOver="rollIn(this)" onMouseOut="rollOut(this)" language="Javascript1.2" href="register.php"> Click Here</a> to Sign Up. Already a member? Then <a href="login.php" style="text-decoration: none" dynamicanimation="fpAnimformatRolloverFP1" fprolloverstyle="text-decoration: overline underline; color: #FF0000; font-weight: bold" onMouseOver="rollIn(this)" onMouseOut="rollOut(this)" language="Javascript1.2"> Click Here</a> to login.</p> <p><b>Net-Petz is best viewed in Internet Explorer and looks bad on Firefox. I'm trying to fix it but I'm new at Firefox. It's best to use Internet Explorer. We have a really cool layout that doesn't work in FF ;_;.</b><br> </p> <table border="4" cellspacing="1" width="706" id="AutoNumber4" height="1" bordercolorlight="#000099" bordercolordark="#000099"> <tr> <td bordercolorlight="#000099" bordercolordark="#000099" align="left" valign="top" width="697" height="5" colspan="2"> <p align="center"><font size="5" face="Comic Sans MS" color="#000099"> News</font></td> </tr> <tr> <td bordercolorlight="#000099" bordercolordark="#000099" align="left" valign="top" width="358" height="91"> <p align="center"><u><b>6th in the Month of the Dragonous ~ New Dragonous </b></u></p> <p align="center">The dragonous has been re-designed and will now appear on the site. Hope you like it! </p> <p align="center"><img src=Dragonous.jpg></p></td> <td bordercolorlight="#000099" bordercolordark="#000099" align="center" width="339" height="91"> <img border="0" src="welcome.jpg" width="300" height="300"></td> </tr> </table> <p> <br> </td> </tr> </table></div></body></html>[/code]Page it goes to when you login:[code]<?include ("config2.php");if ((!$preuserName) || (!$preuserPassword)) { header("Location:http://www.$domain/demo/login.php");exit; }else { $db = mysql_select_db($db_name, $connection) or die("Couldn't select database2."); $sql = "SELECT * FROM chibifriends WHERE preuserName =\"$preuserName\" AND preuserPassword = \"$preuserPassword\" "; $result = mysql_query($sql,$connection) or die("Couldn't execute query."); $query_data = mysql_fetch_array($result); $preuserID = $query_data['preuserID']; $preuserAge = $query_data['preuserAge']; $cimage = $query_data['image']; $userstatus = $query_data['userstatus']; $username = $query_data['preuserName']; $password = $query_data['preuserPassword']; $num = mysql_num_rows($result); $today = date("Y-m-d"); if ($num != 0) { session_start(); session_register('preuserName'); session_register('preuserID'); session_register('cimage'); session_register('preuserAge'); session_register('userstatus'); $session=session_id(); mysql_query("UPDATE chibifriends SET userLastLogin = '$today' WHERE preuserID = '$preuserID'"); $domain = GetHostByName($REMOTE_ADDR); //Cookie$expire = time()+60*60*24*365; // Expire in 1 year setcookie("preuserID", "$preuserID", $expire); setcookie("preuserPassword", "$password", $expire); header("Location: ../home.php"); exit;//end cookie } else { die("There was an error with your login information. Please hit the back button on your browser to fix it or email us at passwords@net-petz.com with your username and email address and we'll send you your password if the information matches. All password requests are handled within 24 hours. Thanks!"); } } ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/ Share on other sites More sharing options...
Jessica Posted January 18, 2007 Share Posted January 18, 2007 Add the path and domain as well"bool setcookie ( string name [, string value [, int expire [, string path [, string domain ..."Path is '/' and the domain is 'yourdomain.com'. Try that.And don't surround vars in "" when you don't need them. Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/#findComment-164050 Share on other sites More sharing options...
Pi_Mastuh Posted January 18, 2007 Author Share Posted January 18, 2007 Like this: bool setcookie ( preuserID [, $preuserID [, $expire [, / [, net-petz.com]]]]);? Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/#findComment-164054 Share on other sites More sharing options...
Crimpage Posted January 18, 2007 Share Posted January 18, 2007 The path is just /basically means that the cookie is valid for all of net-petz.com.You can make it /subfolderand that would mean it is only valid for net-petz.com/subfolder/- Dave Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/#findComment-164058 Share on other sites More sharing options...
Pi_Mastuh Posted January 18, 2007 Author Share Posted January 18, 2007 So is that right? Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/#findComment-164059 Share on other sites More sharing options...
Jessica Posted January 18, 2007 Share Posted January 18, 2007 No, like how you did before, but add those other two arguments.setcookie("preuserID", $preuserID, $expire, '/', 'net-petz.com'); Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/#findComment-164062 Share on other sites More sharing options...
Pi_Mastuh Posted January 18, 2007 Author Share Posted January 18, 2007 Parse error: parse error, unexpected T_STRING in /homepages/20/d175171605/htdocs/reg/secure/index.php on line 47Line 47 is bool setcookie ("preuserID", $preuserID, $expire, '/', 'net-petz.com'); Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/#findComment-164064 Share on other sites More sharing options...
jeka911 Posted January 18, 2007 Share Posted January 18, 2007 Why don't you set values to session variables?$_SESSION["preuserID"] = $preuserid;$_SESSION["prePassword"] = $password;and add something like: ini_set("session.gc_maxlifetime", "18000"); before session_start()... Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/#findComment-164066 Share on other sites More sharing options...
Jessica Posted January 18, 2007 Share Posted January 18, 2007 Take out the freaking bool. bool is what it returns, that's what the manual says. You don't type that when you call it. Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/#findComment-164069 Share on other sites More sharing options...
Pi_Mastuh Posted January 18, 2007 Author Share Posted January 18, 2007 I have session_start(); session_register('preuserName'); session_register('preuserID'); $session=session_id();when you login, but I don't know how to make it not expire when you close the window. I'd like them to last for a year or just forever. Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/#findComment-164070 Share on other sites More sharing options...
Pi_Mastuh Posted January 18, 2007 Author Share Posted January 18, 2007 I took the bool out, nothing's happening. I logged in, closed the page, and opened it and it didn't re-direct. Quote Link to comment https://forums.phpfreaks.com/topic/34808-need-help-with-cookies/#findComment-164072 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.