?
Solved

Session Variable lost when posting Form with PHP

Posted on 2009-05-05
3
Medium Priority
?
308 Views
Last Modified: 2013-12-12
Hi Experts,

I have a wierd problem. The following code is in a file called 'edittxt.php'

When you access the file directly i.e www.domain.com/edittxt.php 

It works perfectly and carries the session variable from the previous window into the form on edittxt.php. It also displays the content of the txt file based on the session value which determines the name of the txt file then presents it to the user for editing. When pressing the submit button it executes the script perfectly and updates the desired txt file as it should whilst also still holding all the session varables. So we have now determined the followng script works perfectly when being executed on it's own.

My problem is when I use the 'includes() command to include the following code as part of another php file to integrate the whole thing together it fails on pressing the 'update/submit' button. The variable is some how lost when pressing the update/submit button.

I receive errors that it cannot process '/home/file/location/.txt' which indicates pretty clearly to me that it losts the session variables when posting the form. It's strange though as I don't get this error when running the script directly.

Many Thanks in advance experts.
<?php
session_start();
if($_POST['Submit']){
$filename = "{$_SESSION['_amember_user']['login']}.txt";
$open = fopen("/home/file/location/$filename","w+");
$text = $_POST['update'];
fwrite($open, $text);
fclose($open);
echo "<font face=verdana size=3 color=grey><b>{$_SESSION['_amember_user']['company']} Hosts File Successfully Updated.</b></tr><br />";
echo "========================================";
echo "<br />";
echo "<font face=verdana size=2 color=black><br />***** HOSTS FILE START *****";
echo "<br />";
echo "<font face=verdana size=2 color=black><br />";
$filename = "{$_SESSION['_amember_user']['login']}.txt";
$file = file("/home/file/location/$filename");
foreach($file as $text) {
echo $text."<br />";
}
// echo "<p><a href=\"./livepage.php\">click here to view the live updated webpage</a></p>";
// echo "<p><a href=\"./mainadminpage.php\">click here to view the admin menu</a></p>";
}else{
$filename = "{$_SESSION['_amember_user']['login']}.txt";
$file = file("/home/file/location/$filename");
echo "<form action=\"".$PHP_SELF."\" method=\"post\">";
echo "<textarea Name=\"update\" cols=\"100\" rows=\"20\">";
foreach($file as $text) {
echo $text;
} 
echo "</textarea>";
echo "<br />";
echo "<br />";
echo "<input name=\"Submit\" type=\"submit\" value=\"Update\" />\n
</form>";
}
?>

Open in new window

0
Comment
Question by:logicsolutions
[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
  • 2
3 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24310497
Make sure you call session_start() before you have any output to the browser, i.e. at the start of the script that you use to include this script.
0
 
LVL 1

Author Comment

by:logicsolutions
ID: 24310565
I have session_start() in both the files.

What I also did for further testing was just copy the code I had into the php webpage I directly and tried to run the script that way. It's not keeping the session correctly when posting. I know that session_start() is working correctly becuase the correct content is loaded upon the initial entry. It's just when you click the update/submit button it all falls over. The other thing I noticed is that it also falls over when you do a browser refresh of the page.

When you access the edittxt.php directly everything works perfectly. It's only when I try to include that code in other pages.
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 2000 total points
ID: 24310669
Are you sure session_start() is executed before any other output? This will fail:

<!DOCTYPE ...
<?php session_start(); ?>
<html>

...and this will fail:

<?php
include('dbconnection.php');
?>
<?php
session_start();
?>

This last example has a linefeed between "?>" and "<?php".

Can you post the other file?
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.

718 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