Solved

PHP and cookie and request

Posted on 2006-07-21
5
394 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
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 20 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
comma true 6 37
How can I make this form submit to itself? 10 35
ability to create nested list on the UI using PHP, HTML, Javascript 7 45
Undefined variable problem 5 22
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

839 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