?
Solved

PHP and cookie and request

Posted on 2006-07-21
5
Medium Priority
?
408 Views
Last Modified: 2006-11-18
I have the followings folders and files

WWW
   |
   |----main.php
   |
   |---member
   |       |----login.php
   |---menu
           |----index.php

WWW, member, menu is a folder.
Why the main.php can not read the cookie that written from the login.php?
How to make it work?

Below is all the code in each files:

// login.php

<?php
setcookie("TestCookie", "abc");
header("Location: ../main.php");
?>

// index.php

<form method="POST" action="member/login.php">
<input type="submit" value="login" name="B3">
</form>
Status:

<?php

if (isset($_COOKIE['TestCookie']))
{
echo 'The value in the cookie is $_COOKIE["TestCookie"]';
}
else
{
echo 'No cookie / Cannot read cookie';
}

?>

// main.php

<table border="1" width="100%" id="table1">
<tr>
<td><?php require ('menu/index.php');?></td>
<td>The left side should display abc.</td>
</tr>
</table>
0
Comment
Question by:mawingpui
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 29

Expert Comment

by:TeRReF
ID: 17159202
Instead of this:
setcookie("TestCookie", "abc");
try this:
setcookie("TestCookie", "abc", time()+60*60*24*30, '/');

time()+60*60*24*30 means that the cookie will be valid for 30 days, so you will have to adjust it if you require another timespan. (60*60*24 is 1 day in seconds, *30)
0
 

Author Comment

by:mawingpui
ID: 17161081
If I use the full path (require function) in the main.php, why it cannot read the cookie that written from login.php?

I change the followings:

// main.php

<table border="1" width="100%" id="table1">
<tr>
<td><?php require ('http://www.mydomain.com/menu/index.php');?></td>
<td>The left side should display abc.</td>
</tr>
</table>

// login.php

<?php
setcookie("TestCookie", "abc", time()+60*60*24*30, '/');
header("Location: ../main.php");
?>
0
 
LVL 9

Accepted Solution

by:
cristi_2112 earned 80 total points
ID: 17175146
I created all the files and directories like you wrote them.
Then I go to http://mysite/member/login.php and I get redirected to http://mysite/main.php and the cookie is seen.
It works like it's supposed to.

Anyway, change:
echo 'The value in the cookie is $_COOKIE["TestCookie"]';

into:
echo 'The value in the cookie is ' . $_COOKIE["TestCookie"];

So you will be able to really see the 'abc' ;)
Is this what you are trying to do ?
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

771 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