Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP and cookie and request

Posted on 2006-07-21
5
Medium Priority
?
416 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
3 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
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…
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 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 …
Suggested Courses
Course of the Month20 days, 19 hours left to enroll

810 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