Cookie questions

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • thetakerfan
    Senior Member
    • Jun 2000
    • 1095

    Cookie questions

    I was working on a tiny script that would use cookies to allow users to my site to change the colors of my site to their liking.
    It works fine, but only on the page that I insert into
    Code:
    <form action="Nav/Header.php" method="post">
    , and nowhere else
    How can I make all the pages on my site read the cookie?
  • Krucifyx
    Senior Member
    • Jun 2000
    • 115

    #2
    You can access any cookie set by your domain by using the $HTTP_COOKIE_VARS variable. So if you named a cookie color, you can access its value using $HTTP_COOKIE_VARS['color'].

    Comment

    • thetakerfan
      Senior Member
      • Jun 2000
      • 1095

      #3
      thanks, I'll try that now

      Comment

      • thetakerfan
        Senior Member
        • Jun 2000
        • 1095

        #4
        still isn't working
        does anyone know a good cookie tutorial, I read what they had at php.net and zend, but perhaps I'm missing something

        Comment

        • Universal Sea
          New Member
          • Oct 2000
          • 25

          #5
          http://www.devshed.com/Server_Side/PHP/ has the best online tutorials. plus, if you post a question on their message board, at least you get a response there, and very quickly at that.

          Comment

          • Krucifyx
            Senior Member
            • Jun 2000
            • 115

            #6
            There is no reason it shouldn't be working unless you are doing something wrong. Make sure the cookie's domain hasn't been restricted to a certain directory.

            Comment

            • thetakerfan
              Senior Member
              • Jun 2000
              • 1095

              #7
              I still can't get other pages to read from it
              The form page shows the changes correctly, but no other pages do

              Comment

              • Krucifyx
                Senior Member
                • Jun 2000
                • 115

                #8
                What exactly are you trying to do? Post code if possible as its hard to give you a definitive answer otherwise.

                Comment

                • thetakerfan
                  Senior Member
                  • Jun 2000
                  • 1095

                  #9
                  well, the code is probably really sloppy, I have to work on cleaning it up, but here it is
                  Code:
                  <?php
                  require ('C:\Lino\Webpage\TWTF-PHP\settings2.inc');
                  // Beginning php
                  
                  // Set up variables that will be saved in the cookies
                  // Define unique cookie prefix
                  $PRE = "TWTF"; 
                  // Set cookie for lifetime
                  $expire = mktime(0,0,0,1,1,2020); 
                  
                  $n_name = $PRE . "_Name"; 
                  $n_email = $PRE . "_Email"; 
                  $n_last = $PRE . "_Last"; 
                  $n_testcookie = $PRE . "_TestCookie"; 
                  $n_bgcolor = $PRE . "_BGColor"; 
                  $n_textcolor = $PRE . "_TextColor"; 
                  $n_linkcolor = $PRE . "_LinkColor"; 
                  $n_alinkcolor = $PRE . "_ALinkColor"; 
                  $n_vlinkcolor = $PRE . "_VLinkColor"; 
                  $n_tablebgcolor = $PRE . "_TableBGColor"; 
                  $n_tableborder = $PRE . "_TableBorder"; 
                  $n_tableheader = $PRE . "_TableHeader";
                  $n_tablemouse = $PRE . "_TableMouse";
                  
                  // This loop treats users who have not been to the site before.
                  if(!$$n_last) {  
                  Setcookie($n_last,Date("H:i d/m/Y"),$expire);
                  ?>
                  <html>
                  <head>
                  	<title><?php echo $sitetitle; ?> > Change Settings</title>
                  </head>
                  <body bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_BGColor']; ?>" text="<?php echo $HTTP_COOKIE_VARS['TWTF_TextColor']; ?>" link="<?php echo $HTTP_COOKIE_VARS['TWTF_LinkColor']; ?>" alink="<?php echo $HTTP_COOKIE_VARS['TWTF_ALinkColor']; ?>" vlink="<?php echo $HTTP_COOKIE_VARS['TWTF_VLinkColor']; ?>" marginheight=0 marginwidth=2 topmargin=2 leftmargin=2 rightmargin=2>
                  <h1 align="center">Settings Area</h1>
                  This is the ThemeGuys Wrestling Theme Foundation settings area.  Feel free to change any of the values below to make the site better suit you.
                  <form action="<?php echo $PHP_SELF ?>" method="POST"> 
                  <table width="90%" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_tableborder']; ?>" border="0" cellspacing="1" cellpadding="2" align="center">
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Name</td>
                  		<td><input type="text" name="name" value="<?php echo $$n_name; ?>"></td>
                  	</tr>
                  <!--- 	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Email</td>
                  		<td><input type="text" name="email" value="<?php echo $$n_email; ?>"></td>
                  	</tr> --->
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Cookie Test</td>
                  		<td><input type="text" name="testcookie" value="<?php echo $$n_testcookie; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Background Color</td>
                  		<td><input type="text" name="bgcolor" value="<?php echo $$n_bgcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Text Color</td>
                  		<td><input type="text" name="textcolor" value="<?php echo $$n_textcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Link Color</td>
                  		<td><input type="text" name="linkcolor" value="<?php echo $$n_linkcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Active Link Color</td>
                  		<td><input type="text" name="alinkcolor" value="<?php echo $$n_alinkcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Visited Link Color</td>
                  		<td><input type="text" name="vlinkcolor" value="<?php echo $$n_vlinkcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Table BG Color</td>
                  		<td><input type="text" name="tablebgcolor" value="<?php echo $$n_tablebgcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Table Border Color</td>
                  		<td><input type="text" name="tableborder" value="<?php echo $$n_tableborder; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Table Mouse Color</td>
                  		<td><input type="text" name="tablemouse" value="<?php echo $$n_tablemouse; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>"><td colspan="2"><input type="submit" name="sfeedback"></td></tr>
                  </table>
                  </form>
                  <br>
                  <?php require ('C:\Lino\Webpage\TWTF-PHP\Default.php'); ?>
                  </body></html>
                  <?php
                          exit;
                      } 
                  	
                  	else {
                          // Set cookies and continue
                          Setcookie($n_name,$name,$expire);
                          Setcookie($n_email,$email,$expire);
                  		Setcookie($n_testcookie,$testcookie,$expire);
                  		Setcookie($n_bgcolor,$bgcolor,$expire);
                  		Setcookie($n_textcolor,$textcolor,$expire);
                  		Setcookie($n_linkcolor,$linkcolor,$expire);
                  		Setcookie($n_alinkcolor,$alinkcolor,$expire);
                  		Setcookie($n_vlinkcolor,$vlinkcolor,$expire);
                  		Setcookie($n_tablebgcolor,$tablebgcolor,$expire);
                  		Setcookie($n_tableborder,$tableborder,$expire);
                  		Setcookie($n_tableheader,$tableheader,$expire);
                  		Setcookie($n_tablemouse,$tablemouse,$expire);
                  		
                  		$$n_name = $name;
                  		$$n_email = $email;
                  		$$n_testcookie = $testcookie;
                  		$$n_bgcolor = $bgcolor;
                  		$$n_textcolor = $textcolor;
                  		$$n_linkcolor = $linkcolor;
                  		$$n_alinkcolor = $alinkcolor;
                  		$$n_vlinkcolor = $vlinkcolor;
                  		$$n_tablebgcolor = $tablebgcolor;
                  		$$n_tableborder = $tableborder;
                  		$$n_tableheader = $tableheader;
                  		$$n_tablemouse = $tablemouse;
                      }
                  
                  // This loop treats repeat users.
                  Setcookie($n_last,Date("H:i d/m/Y"),$expire);
                  ?>
                  <html>
                  <head>
                  	<title><?php echo $sitetitle; ?> > Change Settings</title>
                  </head>
                  <body bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_BGColor']; ?>" text="<?php echo $HTTP_COOKIE_VARS['TWTF_TextColor']; ?>" link="<?php echo $HTTP_COOKIE_VARS['TWTF_LinkColor']; ?>" alink="<?php echo $HTTP_COOKIE_VARS['TWTF_ALinkColor']; ?>" vlink="<?php echo $HTTP_COOKIE_VARS['TWTF_VLinkColor']; ?>" marginheight=0 marginwidth=2 topmargin=2 leftmargin=2 rightmargin=2>
                  <h1 align="center">Settings Area</h1>
                  Welcome back to the ThemeGuys Wrestling Theme Foundation settings area, <?php echo $HTTP_COOKIE_VARS['TWTF_Name']; ?>. Need to change something else? Go right ahead.
                  <?php 
                  // Have previous login
                  if($$n_last)
                  // User fills in feedback form
                  ?>
                  
                  <form action="<?php echo $PHP_SELF ?>" method="POST">
                  <table width="90%" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBorder']; ?>" border="0" cellspacing="1" cellpadding="2" align="center">
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Name</td>
                  		<td><input type="text" name="name" value="<?php echo $$n_name; ?>"></td>
                  	</tr>
                  <!--- 	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Email</td>
                  		<td><input type="text" name="email" value="<?php echo $$n_email; ?>"></td>
                  	</tr> --->
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Cookie Test</td>
                  		<td><input type="text" name="testcookie" value="<?php echo $$n_testcookie; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Background Color</td>
                  		<td><input type="text" name="bgcolor" value="<?php echo $$n_bgcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Text Color</td>
                  		<td><input type="text" name="textcolor" value="<?php echo $$n_textcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Link Color</td>
                  		<td><input type="text" name="linkcolor" value="<?php echo $$n_linkcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Active Link Color</td>
                  		<td><input type="text" name="alinkcolor" value="<?php echo $$n_alinkcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Visited Link Color</td>
                  		<td><input type="text" name="vlinkcolor" value="<?php echo $$n_vlinkcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Table BG Color</td>
                  		<td><input type="text" name="tablebgcolor" value="<?php echo $$n_tablebgcolor; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Table Border Color</td>
                  		<td><input type="text" name="tableborder" value="<?php echo $$n_tableborder; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<? echo $tdmouseover; ?>Table Mouse Color</td>
                  		<td><input type="text" name="tablemouse" value="<?php echo $$n_tablemouse; ?>"></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>"><td colspan="2"><input type="submit" name="sfeedback"></td></tr>
                  </table>
                  </form>
                  <br>
                  <?php require ('C:\Lino\Webpage\TWTF-PHP\Default.php'); ?>
                  </body></html>
                  I made a page to test to see if I could read from the cookies, which isn't happening, heres the code for that page
                  Code:
                  <table width="90%" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_tableborder']; ?>" border="0" cellspacing="1" cellpadding="2" align="center">
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Name</td>
                  		<td><?php echo $HTTP_COOKIE_VARS['TWTF_Name']; ?></td>
                  	</tr>
                  <!--- 	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Email</td>
                  		<td><input type="text" name="email" value="<?php echo $$n_email; ?>"></td>
                  	</tr> --->
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Cookie Test</td>
                  		<td><?php echo $HTTP_COOKIE_VARS['TWTF_CookieTest']; ?></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Background Color</td>
                  		<td><?php echo $HTTP_COOKIE_VARS['TWTF_BGColor']; ?></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Text Color</td>
                  		<td><?php echo $HTTP_COOKIE_VARS['TWTF_TextColor']; ?></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Link Color</td>
                  		<td><?php echo $HTTP_COOKIE_VARS['TWTF_LinkColor']; ?></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Active Link Color</td>
                  		<td><?php echo $HTTP_COOKIE_VARS['TWTF_ALinkColor']; ?></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Visited Link Color</td>
                  		<td><?php echo $HTTP_COOKIE_VARS['TWTF_VLinkColor']; ?></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Table BG Color</td>
                  		<td><?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Table Border Color</td>
                  		<td><?php echo $HTTP_COOKIE_VARS['TWTF_TableBorder']; ?></td>
                  	</tr>
                  	<tr align="left" bgcolor="<?php echo $HTTP_COOKIE_VARS['TWTF_TableBGColor']; ?>">
                  		<td>Table Mouse Color</td>
                  		<td><?php echo $HTTP_COOKIE_VARS['TWTF_TableMouse']; ?></td>
                  	</tr>
                  </table>

                  Comment

                  • Krucifyx
                    Senior Member
                    • Jun 2000
                    • 115

                    #10
                    I tested parts of it and it worked for me. Save this little code bit as test.php or whatever AFTER you run the script in question and see if any cookies are being set:

                    Code:
                    <?
                    
                    while (list($key,$value) = each($HTTP_COOKIE_VARS)) {
                      print "$key - $value<br>";
                    }
                    
                    ?>
                    All this:
                    Code:
                    $n_name = $PRE . "_Name"; 
                    $n_email = $PRE . "_Email"; 
                    $n_last = $PRE . "_Last"; 
                    $n_testcookie = $PRE . "_TestCookie"; 
                    $n_bgcolor = $PRE . "_BGColor"; 
                    $n_textcolor = $PRE . "_TextColor"; 
                    $n_linkcolor = $PRE . "_LinkColor"; 
                    $n_alinkcolor = $PRE . "_ALinkColor"; 
                    $n_vlinkcolor = $PRE . "_VLinkColor"; 
                    $n_tablebgcolor = $PRE . "_TableBGColor"; 
                    $n_tableborder = $PRE . "_TableBorder"; 
                    $n_tableheader = $PRE . "_TableHeader";
                    $n_tablemouse = $PRE . "_TableMouse";
                    is completely unnecassary. You only use it once to set the cookie and not to retreive, so I see no need to use it. Just more unneeded variables that cluters things up.

                    Comment

                    • thetakerfan
                      Senior Member
                      • Jun 2000
                      • 1095

                      #11
                      You aren't gonna believe this, it was all just a DUMB mistake all along.
                      I didn't notice that on my computer(where I've been testing this out), the URL changes once something is executed in a PHP script from
                      "/themeguys/TestCookie.php" to "/php/php.exe/themeguys/TestCookie.php", so because of the dif paths, the cookies weren't working right.

                      Is there any way to keep the paths from changing?

                      (I've done a bit of cleaning up to the code as well, which I had been meaning to do)

                      Comment

                      • thetakerfan
                        Senior Member
                        • Jun 2000
                        • 1095

                        #12
                        I'm sure this, as well as most of my others, is a pretty dumb question, but how can I set a cookie to be read from all paths?
                        The script that sets the cookies is in the "/Cookie/" path, and I need it run from many paths

                        Comment

                        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
                        Working...