Solved

Session Variable lost when posting Form with PHP

Posted on 2009-05-05
3
299 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
  • 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 500 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

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

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

760 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

19 Experts available now in Live!

Get 1:1 Help Now