?
Solved

My cookie is not writing what am i doing wrong?

Posted on 2007-08-08
5
Medium Priority
?
159 Views
Last Modified: 2008-03-06
I have a page for login and this page processes the page after submitting but it is not working it will not write the cookies.
<?php
$STAFFNAME=$_POST['STAFFNAME'];
$NSKPasswd=$_POST['NSKPasswd'];
$STAFFDEPT=$_POST['STAFFDEPT'];
if($NSKPasswd != "lsss") {
      die ('<font size="+4" color="Red"><b>!</b></font><font size="+3">You must have the correct password to enter this site. Please use your back button and try again.</font>');}
?>
<?
setcookie ("STAFFID",$STAFFNAME,"30");
setcookie ("STAFFDEPT",$STAFFDEPT,"30");
?>
<?
if  ($STAFFNAME == "Guest") { ?>
<center>
<table bgcolor="white" border="0" width="45%" bordercolordark="red" bordercolorlight="teal">
<tr><td colspan=2 align="center"><h2>Choose a Page</h2></td></tr>
<tr align="center">
      <td><font size="+2" color="Black"><a href="/ServiceRequest/admin/viewallCases.cfm">View All Cases Page</a></font></td>
</tr>
</table>
</center>
</body>
</html>
<? } ?>



<?
if($STAFFNAME == "LHaluska" || $STAFFNAME == "BSINGH"){ ?>

<center><table bgcolor="white" border="0" width="45%" bordercolordark="red" bordercolorlight="teal"><tr><td colspan=2 align="center"><h2>Choose a Page</h2></td></tr><tr align="center"><td><font size="+2" color="Black"><a href="/ServiceRequest/admin/managers/default.php">Managers Page</a></font></td>
      <td><font size="+2" color="Black"><a href="/ServiceRequest/admin/openCase.cfm">Staff Page</a></font></td>
</tr>
</table>
</center>
</body>
</html>
<? }
?>
<center>
<img src="/images/passed.jpg" width=302 height=284 alt="Passed" border="0">
<br>
</center>

<?

$dbc = mssql_connect ('cacapppro01.gmail.com','nskinfo','webres');
$staffstatus = mssql_query("SELECT Name, Status, Dept
      FROM         StaffList
      WHERE       (Name = '$STAFFNAME') AND (Dept = '$STAFFDEPT')");

if ($staffstatus == "OUT") { ?>
      <h1 align="center">You are currently listed as OUT on the out agent</h1>
      <script language="JavaScript">
      window.location="/ServiceRequest/admin/outagent.cfm";
      </script>
<? }      
else {
?>      
<script language="JavaScript">
      window.location="/ServiceRequest/admin/openCase.cfm";
      </script>
<?
}
?>
<?
mssql_close($dbc);
?>

            <br><a href="/ServiceRequest/admin/openCase.cfm" class="roll">
            <font size="+1" color="Red"><center>Click here to continue</center></font></a>

</body>
</html>
0
Comment
Question by:lhaluska
  • 2
  • 2
5 Comments
 
LVL 15

Expert Comment

by:ncoo
ID: 19655647
Cookies need a longer life. Try:

setcookie("STAFFID", $STAFFNAME, time()+3600);
setcookie("STAFFDEPT", $STAFFDEPT, time()+3600);

This will mean the live for an hour.
0
 
LVL 1

Author Comment

by:lhaluska
ID: 19655756
How would I make it so that it will last for 30 days?
0
 
LVL 2

Expert Comment

by:etully
ID: 19655928
(That number is in seconds.  A month is 2600000 seconds).

But I think there is another problem besides the 30.  You see the line where you say

?>
<?

You are leaving PHP and then going back into PHP.  There is a line break between those two tags.  You can't set cookies after you print anything to the screen and you just printed a line break.  Simply remove those two lines.

Also,  You should stop using <? to indicate PHP code.  It's important to start using <?php instead.  It might not seem like a big deal since your code is working fine with <? but someday you are going to want to move this code to a different web hosting company and they will be using the newer, more correct <?php tag instead.


0
 
LVL 1

Author Comment

by:lhaluska
ID: 19655956
I was wondering why some of my pages write to 2 different cookies one named "haluskal@ServiceRequest.txt" and the other "haluskal@qebrec.web.cupcorp.txt" the page above writes to the "ServiceRequest.txt" one. How do I only write to the "qebrec.web.cupcorp.txt".
0
 
LVL 15

Accepted Solution

by:
ncoo earned 2000 total points
ID: 19656654
<?php
$life=time()+7776000;//lives 3 months
setcookie("STAFFID", $STAFFNAME, $life "/", ".qebrec.web.cupcorp.txt");
setcookie("STAFFDEPT", $STAFFDEPT,$life, "/", ".qebrec.web.cupcorp.txt");
?>

setcookie("COOKIENAME","VALUE","LIVE", "DIRECTORY", "DOMAIN");

Set directory and domain for where the cookie can be used.
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month14 days, 22 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question