Solved

PHP and cookie and request

Posted on 2006-07-21
5
398 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 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to count occurrences of each item in an array.
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 …

733 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