Solved

PHP and cookie and request

Posted on 2006-07-21
5
387 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
5 Comments
 
LVL 29

Expert Comment

by:TeRReF
Comment Utility
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
Comment Utility
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 20 total points
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

This article discusses four methods for overlaying images in a container on a web page
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now