473,387 Members | 1,486 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Lost session across folders

58
Good morning everyone

I wonder if anyone encountered this situation before.
I have a 2 pages form inside a folder.The second one processes and echoes any errors to page1(thru a redirect).Session data is passed to and fro without using a session id.

All is working fine so far

Now I moved page2 (the processing script) to another folder and correctly modified the path to it.

Now the errors (a session error array) is now longer echoing to page1.I checked the database and data is submitted successfully,Normally there is a message of success or errors,but not anymore.

Could it be because am not using a session id ?

I appreciate any help i can get
Aug 16 '07 #1
11 3924
ak1dnar
1,584 Expert 1GB
Sorry Jankie, Quistion is Not That much clear(to me). may be with your Codings It is easy for me to understand. Can you post them here.
Aug 16 '07 #2
Jankie
58
Page1(form)

[php]
<?php
session_start();
foreach($_POST as $key => $val)
$_SESSION[$key] = $val;
?>
<!DOCTYPE phpl PUBLIC "-//W3C//DTD XphpL 1.0 Transitional//EN"
"http://www.w3.org/TR/xphpl1/DTD/xphpl1-transitional.dtd">
<phpl lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xphpl">
<head>
<meta http-equiv="Content-type" content="text/phpl; charset=utf-8" />
<title>page1</title>
</head>
<body>

<?php
if(isset($_SESSION['submit']) && $_FILES['userfile']['size'] > 0){
include 'Includes/DbConnect.inc.php';// includes folder in same parent folder as page1.php

$ID = $_POST['ID'];
$fname = $_POST['fname'];
$lstame = $_POST['lstame'];
$email = $_POST['email'];

if(!empty($_SESSION['errmsg'])) {
print('<div class="errmsg">' . implode('<br />', $_SESSION['errmsg']) . '</div>');
unset($_SESSION['errmsg']);
}
?>
<FORM action="OtherFolder/page2.php" method='post' enctype='multipart/form-data' name='page1' >
..rest of form
</form>
</body>
</phpl>
[/php]

Page2(processing script)

[php]

<?php
session_start();
foreach($_POST as $key => $val)
$_SESSION[$key] = $val;
header ("Location: http://www.mysite.com/page1.php"); // page1 is outside page2 folder
if(isset($_POST['submit'])){
include '../Includes/DbConnect.inc.php';

$ID = $_POST['ID'];
$fname = $_POST['fname'];
$lstame = $_POST['lstame'];
$email = $_POST['email'];

$_SESSION['errmsg'] = array();

if (!$_SESSION['fname'])
{
$_SESSION['errmsg'][] = 'Error:Please enter your first name';
}
.............
.............
if(!empty($_SESSION['errmsg']))
exit();
else {
$sql = "INSERT INTO table(ID,,fname','lstname','email',) VALUES ($ID,'$fname','$lstname','$email',)";
if (!mysql_query($sql,$conn))
{
$_SESSION['errmsg'][] = mysql_error();
exit();
}
else
{
$ID=mysql_insert_id();
$_SESSION['errmsg'][] = "Your information was successfully submitted";
foreach(array('ID',fname','lstname','email', 'error') as $key)
unset($_SESSION[$key]);
}
mysql_close($conn);
}
?>
[/php]


When I have page2.php in the same folder as page1.php,messages are echoed . When i redirected to page1.php from another folder where page2.php resides,no messages are echoed.

Thank you very much for investing any time/effort in having a look a this request of mine !
Aug 16 '07 #3
ak1dnar
1,584 Expert 1GB
I didn't get a chance to debug your code, but while I am overlooking your coding, I got some erroneous point.
As I can feel Page2 is the Processing Script to your Page1. So After Validating User inputs you are trying to redirect back to the Parent page with the Generated Errors or any other Messages.
But Just refer to this line 5. what will happen here now, Before execute the rest of the coding after this line your page will redirect Back to the Parent page.

Expand|Select|Wrap|Line Numbers
  1. header ("Location: http://www.mysite.com/page1.php");
You have to put this end of the page2.

And I more thing I should tell. Sessions are available to all pages in one application. There is Nothing to worry about the Folders.
Aug 16 '07 #4
Jankie
58
Thank you ajaxrand for your debugging !
I dont understand your comment though
I have no exit after the header so the script continues excuting and inserts into the db.
I made the success message also a error array .And because i have a conditional that if error array is not empty(there's an error) it must exit.This exit with the header above forces it to redirect and echo the error in page1.
There is an exit only if there's an error(but because success message is also coded as error) all messages will be redirected and echoed on page1.
As i mentioned,when i have the 2 pages whitin one folder,everything works fine(scripts excutes,inserts and echoes errors) .It's only when the proccessing script has to redirect outside the folder. I put a test on the existence of a session on page2 and it echoed "session was closed"
I'll apply your suggestion and hope I'' find the solution
Aug 16 '07 #5
ak1dnar
1,584 Expert 1GB
Thank you ajaxrand for your debugging !

I have no exit after the header so the script continues excuting and inserts into the db.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. header('Location: another.page.php'); 
  3. echo 'Can you See this Line there is NO exit() here';
  4. ?>
Aug 16 '07 #6
Jankie
58
Putting the header at the end of page2 made no difference.
$_SESSION['errmsg'] no longer echoes back to page1.
When I bring page2 back to the same folder as page1,$_SESSION['errmsg'] echoes properly(regardless of where i put the header)
Aug 16 '07 #7
ak1dnar
1,584 Expert 1GB
Putting the header at the end of page2 made no difference.
$_SESSION['errmsg'] no longer echoes back to page1.
When I bring page2 back to the same folder as page1,$_SESSION['errmsg'] echoes properly(regardless of where i put the header)
Post the Complete Page1 and Page2 Codings here.
Aug 17 '07 #8
ak1dnar
1,584 Expert 1GB
I was wrong there is NO "echo" statement on Page2 so your page will redirect back to Page1. I was in a So hurry Last Time.

Back to The Thread:

Again I am Telling There is Nothing to worry about the Folders when dealing with Sessions. Thats really Strange to me. May Be I am Wrong Here Too.

We Will try a Simple Working Script That made Based On your Page1, Page2.

Page1.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. foreach($_POST as $key => $val)
  4. $_SESSION[$key] = $val;
  5. ?>
  6. <html>
  7. <body>
  8.  
  9. <?php
  10. //if(isset($_SESSION['submit']) && $_FILES['userfile']['size'] > 0){
  11. //include 'Includes/DbConnect.inc.php';// includes folder in same parent folder as page1.php
  12. $ID = $_POST['ID'];
  13. $fname = $_POST['fname'];
  14. $lstame = $_POST['lstame'];
  15. $email = $_POST['email'];
  16.  
  17. if(!empty($_SESSION['errmsg'])) 
  18. {
  19. print('<div class="errmsg">' . implode('<br />', $_SESSION['errmsg']) . '</div>');
  20. unset($_SESSION['errmsg']);
  21. }
  22. ?>
  23. <FORM action="OtherFolder/page2.php" method="post" enctype="multipart/form-data" name="page1" >
  24. FirstName<input name="fname" type="text" /><br>
  25. LastName<input name="lname" type="text" />
  26. <input name="submit" type="submit" />
  27. </form>
  28. </body>
  29. </html>
  30.  
  31.  
  32.  
Page2.php Inside OtherFolder

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. session_start();
  3. foreach($_POST as $key => $val){
  4. $_SESSION[$key] = $val;
  5. header ("Location: ../page1.php");// Set The Absolute URL here
  6. }
  7.  
  8.     if(isset($_POST['submit'])){
  9.         $_SESSION['errmsg'] = array();
  10.  
  11.         if (!$_SESSION['fname']){
  12.             $_SESSION['errmsg'][] = 'Error:Please enter your first name';
  13.         }
  14.         if (!$_SESSION['lname']){
  15.             $_SESSION['errmsg'][] = 'Error:Please enter your Last name';
  16.         }
  17.         if(!empty($_SESSION['errmsg'])){
  18.         // Still There Are Errors So this will Execute
  19.         }else{
  20.         // Do the Database Inserting Stuffs Here and Print The Session Based Messages 
  21.         }
  22. }
  23. ?>
  24.  
Folder Structre is here

ParentDIR--
Page1.php
OtherFolder--
Page2.php
Just Re-Structre your Code, Once you try this.
Aug 17 '07 #9
Jankie
58
Thank you ajaxrand for all your time and effort you put debugging my code.
There is nothing yet to convince me that sessions cannot be lost when redirecting across folders.So,am searching in that area.May be it's IE7 related.I'll see if on other browsers it's the same or not.
When i redirect to a page with no sessions,the redirect works,when i redirected either backwards or forwards outside a folder to a session page,sessions are lost and i got either a blank page or redirected to login page.
I used a test session snippet i found on php.net and the test proved that session is lost

[php]

$_SESSION['testMe']='test';
@session_start();
if(isset($_SESSION['testMe']))
{
echo "Session was open\n";
unset($_SESSION['testMe']);
}
else
echo "Session was closed\n";

[/php]
In my case,it echoed "Session was closed"

When i remove some cache prevention code,I no longer get the blank page,instead am redirected to Login page.When i remove the session checking
code:
[php]
session_start();
if ((!$_SESSION['Username']) && (!$_SESSION['pass']))
{
//redirect
}
[/php]
I log in,with "Welcome+..forget your name..!

Am not sure,I may be wrong
Aug 18 '07 #10
ak1dnar
1,584 Expert 1GB
Change the First Code Snippet Like this and uncomment and comment the Line Number 4 Then you Will Understand about unset()
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2. session_start();
  3. $_SESSION['testMe']='test';
  4. //unset($_SESSION['testMe']);
  5. if(isset($_SESSION['testMe']))
  6. {
  7. echo "Session was open\n";
  8. }else
  9. {
  10. echo "Session was closed\n";
  11. }
  12. ?>
Aug 20 '07 #11
What version of php are you using? I am having the exact same problem...

I am using php 5.2.5 and I break sessions when calling them across different folders...

It is really picky too... I can make another folder dump the code in and it won't break... it seems to be random when choosing which folders it will or will not break the session with...

Example... I put the code on a php 5.2.4 server and it works...

and I have been coding for 5-6 years... there is nothing wrong with my php... there is either something wrong with the server itself or the php distribution...

I can't figure it out...

please please please let me know which version of php you are running
... I am trying to figure out how to get my server admins to fix the problem...
Jan 24 '08 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: | last post by:
What could the likely cause of a session value being lost when one navigates to another page and attempts to write that session out: Page 1 session("u_name") = oRS.fields("u_name") Navigate...
1
by: Brian Schloz | last post by:
Hello, I just thought I'd share my particular situation regarding session state periodically being "lost" in my asp.net app. I read with interest all of the posts regarding lost session state...
4
by: Bill Dodd | last post by:
Is there really no way to have different asp.net applications share session variables? The problem I'm running into is that I have numerous asp.net (and asp) applications that were written as...
3
by: =?Utf-8?B?UHJpeWE=?= | last post by:
Hi, When i execute application in debug mode it works but on release mode the session values in the website is lost. I use windows server 2003 and IIS 6. It is a 5 step enrollment process whose...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.