Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cookies

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
GB
Hi. Could someone plase tell me why the following code;

setcookie("TEMPCOOKIE", $admin_password_sent, time() + 60 * 60);

$show = $HTTP_COOKIE_VARS["TEMPCOOKIE"];

echo "Password saved was $show";

gives me as PHP error as follows;

Warning: Cannot add header information - headers already sent by (output started at c:\program files\nusphere\apache\nsdocs\test\acepassword\acepassword.php:31) in c:\program files\nusphere\apache\nsdocs\test\acepassword\acepassword.php on line 33

Thanks

Andy
 
YOu must set cookies BEFORE the opening HTML tag.

so it should look like this:

setcookie("TEMPCOOKIE", $admin_password_sent, time() + 60 * 60);

$show = $HTTP_COOKIE_VARS["TEMPCOOKIE"];

<HTML>
<HEAD>
<TITLE></TITLE>
</HEAD>
<BODY>
echo &quot;Password saved was $show&quot;;
</BODY>
</HTML>

 
So basically, I need to set the cookie earlier, and then call it when running the script and checking the cookie?

Thanks

Andy
 
Ok...still aint working :(

Im using the following codes in two different files, template.inc.php and acepassword.php. The codes are;

-----------acepassword.php -----------------------
Code:
<?

require(&quot;template.inc.php&quot;);

$admin_password = &quot;1234&quot;;


$rating_code = $rating;

global $admin_password, $action, $admin_password_sent, $rating_code;

// now we need to call a sub to see what we want to do...
decide_what_to_do($action);

/* ############################ */

// decide what we want to do...
function decide_what_to_do($action) {

if ($action == &quot;login&quot;) { do_login(); } else { login_page(); }

}
/* ############################ */

// sub to check if the username and stuff is correct. If it is we need to create a cookie
// and also read the .htaccess file list so they can decide what area the want to be working
// on.
function do_login() {

if ($GLOBALS[admin_password_sent] == $GLOBALS[admin_password]) {
echo &quot;pasword matched&quot;;

$cookie = setcookie(&quot;TEMPCOOKIE&quot;, $admin_password_sent, time() + 60 * 60);

// $show = $HTTP_COOKIE_VARS[&quot;TEMPCOOKIE&quot;];

echo &quot;Password saved...&quot;;



} else {
echo &quot;<center><font color=\&quot;#FF0000\&quot;>Password $GLOBALS[admin_password_sent] was incorrect</font></center><BR><BR>&quot;;
show_html_login();
}

}

/* ############################ */

// the page to show if nothing was sent..i.e they had not logged in!
function login_page() {

// get the html from template.inc.php..makes this file less messy and easier to
// interpret and fix bugs :p
show_html_login();


}

/* ############################ */

// error sub incase we get some problems...
function error($error) {

echo &quot;There was an error. It was: &quot;;
echo $error;

}

/* ############################ */

?>

----------Templates.inc.php-------------------------
Code:
<?

// html to show when loggin in...
function show_html_login() {

echo <<<TEMPLATE

   <html>

<head>
<meta http-equiv=&quot;Content-Language&quot; content=&quot;en-gb&quot;>
<meta http-equiv=&quot;Author&quot; content=&quot;Ace-Installer.com&quot;>
<meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=windows-1252&quot;>
<title>Ace Password (.htaccess Generator)</title>

<STYLE type=text/css>A:link {
        TEXT-DECORATION: none
}
A:visited {
        TEXT-DECORATION: none
}
A:hover {
        TEXT-DECORATION: none
}
</STYLE>

<STYLE fprolloverstyle>A:hover {color: #ff0000}
</STYLE>

</head>
<body>

<div ALIGN=&quot;left&quot;>
  <table BORDER=&quot;0&quot; CELLPADDING=&quot;0&quot; CELLSPACING=&quot;0&quot; WIDTH=&quot;100%&quot; HEIGHT=&quot;38&quot;>
    <tr>
      <td WIDTH=&quot;100%&quot; BGCOLOR=&quot;#000080&quot; COLSPAN=&quot;3&quot; HEIGHT=&quot;20&quot;>
        <p ALIGN=&quot;center&quot;><b><font SIZE=&quot;1&quot; color=&quot;#000080&quot;>-</font><font COLOR=&quot;#FFFFFF&quot;><BR>
        </font><a HREF=&quot;[URL unfurl="true"]http://www.ace-installer.com&quot;[/URL] TARGET=&quot;_blank&quot;>
        <font FACE=&quot;Verdana&quot; COLOR=&quot;#FFFFFF&quot;>Ace Password PHP v1</font></a><font COLOR=&quot;#FFFFFF&quot;><BR>
        </font><font SIZE=&quot;1&quot; COLOR=&quot;#000080&quot;>-</font></b></td>
    </tr>
    <tr>
      <td WIDTH=&quot;20%&quot; BGCOLOR=&quot;#000080&quot; HEIGHT=&quot;18&quot; VALIGN=&quot;top&quot;>
        <p ALIGN=&quot;center&quot;><b><font color=&quot;#FFFF00&quot; FACE=&quot;Verdana&quot; SIZE=&quot;2&quot;>MENU</font></b></p>
        <p ALIGN=&quot;center&quot;><font COLOR=&quot;#FFFFFF&quot; FACE=&quot;Verdana&quot; SIZE=&quot;2&quot;><b>You
        can't see any<BR>
        options yet as you<BR>
        are not logged in!</b></font></p>
        <p ALIGN=&quot;center&quot;> </p>
        <p ALIGN=&quot;center&quot;> </td>
      <td WIDTH=&quot;56%&quot; HEIGHT=&quot;18&quot; VALIGN=&quot;top&quot;>
        <p ALIGN=&quot;center&quot;> </p>
        <p ALIGN=&quot;center&quot;><font FACE=&quot;Verdana&quot; SIZE=&quot;2&quot;>Please enter your admin
        password to gain access to the control panel.</font></p>
        <form METHOD=&quot;POST&quot; ACTION=&quot;acepassword.php&quot;>
          <p ALIGN=&quot;center&quot;><input TYPE=&quot;password&quot; NAME=&quot;admin_password_sent&quot; SIZE=&quot;20&quot;  style=&quot;color: #080000; border: groove; font-family: Verdana; font-size: 10px&quot;> 
          <input TYPE=&quot;submit&quot; VALUE=&quot;Submit&quot; NAME=&quot;B1&quot; style=&quot;color: #080000; border: groove; font-family: Verdana; font-size: 10px&quot;></p>
          <input TYPE=&quot;hidden&quot; NAME=&quot;action&quot; VALUE=&quot;login&quot;>
        </form>
        <p ALIGN=&quot;center&quot;> </td>
      <td WIDTH=&quot;24%&quot; BGCOLOR=&quot;#808080&quot; HEIGHT=&quot;18&quot; VALIGN=&quot;top&quot;>
        <div ALIGN=&quot;left&quot;>
          <table BORDER=&quot;3&quot; CELLPADDING=&quot;0&quot; CELLSPACING=&quot;0&quot; WIDTH=&quot;100%&quot; BORDERCOLOR=&quot;#000080&quot; HEIGHT=&quot;100%&quot; BGCOLOR=&quot;#008000&quot;>
            <tr>
              <td WIDTH=&quot;100%&quot;>
                <p ALIGN=&quot;center&quot;><b><font COLOR=&quot;#FFFFFF&quot; FACE=&quot;Verdana&quot; SIZE=&quot;2&quot;>Rate This Script</font></b></p>
                <p ALIGN=&quot;center&quot;> $GLOBALS[rating_code]</p>
</td>
            </tr>
          </table>
        </div>
      </td>
    </tr>
  </table>
</div>

<p ALIGN=&quot;center&quot;><a HREF=&quot;[URL unfurl="true"]http://www.ace-installer.com/acepassword.html&quot;[/URL] TARGET=&quot;_blank&quot;><font color=&quot;#000000&quot; FACE=&quot;Verdana&quot; SIZE=&quot;2&quot;>Ace Password PHP V1
by Ace Installer. 1999-2002.</font></a></p>

</body>

</html>

TEMPLATE;

} // end the login page template code...

/* ################################### */

/* This code is not meant to be in a sub. It is so that the script can call it ok...otherwise we have problems*/

$rating = <<<RATING

<div align=&quot;center&quot;>
  <center>
  <table border=&quot;0&quot; cellspacing=&quot;0&quot; height=&quot;83&quot;>
    <tr>
      <td align=&quot;center&quot;><font face=&quot;arial, verdana&quot; size=&quot;2&quot;><b><font color=&quot;#FFFF00&quot;>Rate
        Our Program</font></b><br>
        <a href=&quot;[URL unfurl="true"]http://www.hotscripts.com&quot;[/URL] target=&quot;_blank&quot;><small><font color=&quot;#FFFFFF&quot;>at
        HotScripts.com</font></small></a></font></td>
    </tr>
    <tr>
      <td>
        <table border=&quot;0&quot; cellspacing=&quot;0&quot; width=&quot;100%&quot;>
          <tr>
            <td width=&quot;100%&quot; align=&quot;center&quot;><select name=&quot;ex_rate&quot; size=&quot;1&quot; style=&quot;color: #080000; font-family: Verdana; font-size: 10px&quot;>
                <option value=&quot;5&quot; selected>Excellent!</option>
                <option value=&quot;4&quot;>Very Good</option>
                <option value=&quot;3&quot;>Good</option>
                <option value=&quot;2&quot;>Fair</option>
                <option value=&quot;1&quot;>Poor</option>
              </select></td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td align=&quot;center&quot;><input type=&quot;submit&quot; value=&quot;Cast My Vote!&quot; style=&quot;color: #080000; font-family: Verdana; font-size: 10px&quot;></td>
    </tr>
  </table>
  </center>
</div>
                </form>

RATING;

// end the ratings code... not meant to be in a sub

?>

I'm still gettting that errror with this code :(

Anyone got any more ideas? I havnt set headers anywhere...so would that make a difference?

Thanks

Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top