/    Sign up×
Community /Pin to ProfileBookmark

sessions not being stored after form post

Okay, I am starting to attribute this issue to some setting (php.ini?) on my host’s (1and1.com) server, but figured I would ask you guys…

In Firefox or Safari, I think even IE8, if I clear out all of the browser’s history, close the browser, reopen the browser, and then attempt to submit a form (method=post), the form posts and runs some SQL INSERT AND UPDATE queries just fine; data is stored as expected. [B]The problem is that sessions that I attempt to set are not being set.[/B]

HOWEVER, if I leave the browser open, clear out all of the browser’s history, (again, leaving the browser open), and then re-attempt to submit that same form, the sessions are saved, and everything works fine.

I am confident that the code is fine. I have spent the past couple of days trying to figure this out, and would greatly appreciate ANY input if you have realized this behavior.

Thanks,

bubbis

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@Jarrod1937Feb 02.2011 — When is the session information set? After the form using the forms info? Can you show some code?
Copy linkTweet thisAlerts:
@bubbisthedogauthorFeb 02.2011 — First page where form is submitted (imagine it's page1.php):

[code=php]
session_start();

if (isset($_POST['this'])) {
//connect to database
/*connect stuff goes here...*/

/*bunch of INSERT and UPDATE statements here that work fine, because the data is entered into the database perfectly...*/

/*****not being set is this session*****/
$_SESSION['valid'] = '1';

//go to page 2, where $_SESSION['valid] is used...
die(header('Location: http://www.page2.php'));

mysql_close($con);
}[/code]


Form javascript validation works perfectly, by the way...

[code=html]<form method="post" action="page1.php" onsubmit="var ContinueSubmit = validateForm(); if (ContinueSubmit == 'stop') { focusField('UserNameDetail'); return false; } else { return true; }">

<input id="EmailDetail" name="EmailDetail" type="text" value="" maxlength="100" />

<p id="EmailPrivacyNotice">Your e-mail address is known only by you and the system.</p>

<div id="SubmitChangesContainer">
<input id="SubmitChangesButton" type="Submit" value="Next" />
</div>
</form>[/code]


Nothing new here, really, I know... And, like I said, it works perfectly (the session is set) after I try it once, no matter what the browser is. But when I clean out the history, close and reopen the browser, it refuses to set the session until I try it a second time... ???
Copy linkTweet thisAlerts:
@Jarrod1937Feb 02.2011 — I'm assuming this may have something to do with the session cookie as there is nothing else that persist past the browser restart. In firefox, after cleaning out the history (which i assume means deleting the cookies as well), try pulling up all cookies from your domain (under "tools" -> "Options" -> "Privacy" -> "Use Custom Settings For History" -> "Cookies" -> Filter By Your Domain) and make sure the session cookie doesn't exist.
Copy linkTweet thisAlerts:
@criterion9Feb 02.2011 — Sessions by default expire when the cookie is destroyed or when the browser is closed. Are you wanting longer term storage?
Copy linkTweet thisAlerts:
@Jarrod1937Feb 02.2011 — Sessions by default expire when the cookie is destroyed or when the browser is closed. Are you wanting longer term storage?[/QUOTE]

I interpret the issue as follows:

1.) If he starts with a blank slate (meaning all history, cookies, cache...etc cleared), then closes the browser, opens it, then it takes 2 tries of filling out the form for it to set $_SESSION['valid']=1;

2.) If he starts with a browser that has already been opened, form filled out and session variable set, then clears the "history" (aka cookies, i assume) then it only takes one try to fill out the form and set the session variable.

From that explanation, i'd gather that something is either going weird with the session cookie, or the logic of the app. However, looking at the code, it looks fine to me... however, i would be interested if you could post the code from "page2.php", as that may be where the logical error is. Its possible that it always takes two times to set the session variable, but when you think you're clearing out the history (and thus the cookie), that you may not be (sometimes browsers require you to close them for the cookies to be completely cleared). Thus your #2 from above may actually be you already using a valid session variable and so it only takes one time. However, its hard to say without seeing more code.
Copy linkTweet thisAlerts:
@bubbisthedogauthorFeb 03.2011 — First of all, thanks a lot for taking the time to help, guys. 'page2.php' just checks the $_SESSION['valid'] session to ensure that it's set. If it's not, you're taken back to the login page. Below is literally the only code in 'page2.php' that uses this session:

[code=php]if (!isset($_SESSION['valid'])) {
die(header('Location: http://www.page1.php'));
}[/code]

I've also commented out the 'die' part and tested printing the session to see if the value was set, sort of like this:

[code=php]if (!isset($_SESSION['valid'])) {
echo 'sessionset!';
//die(header('Location: http://www.page1.php'));
}[/code]

It's clear that when the session is not set 'sessionset' prints on the screen; when it's set it isn't printed.

I really don't think it's the code, because it really is as simple as I've stated. It's got to be sessions and cookies not syncing up, or some setting in php.ini from the host (1and1.com) that's making things screwy. It wouldn't surprise me if it's the host: I can't even use 'includes' that include sessions because of host php settings.

I'm at a loss...
Copy linkTweet thisAlerts:
@criterion9Feb 03.2011 — Just out of curiosity...do you have session_start at the top of page2?
Copy linkTweet thisAlerts:
@bubbisthedogauthorFeb 03.2011 — Just out of curiosity...do you have session_start at the top of page2?[/QUOTE]

Yep, at the start of every page on my site, actually.
×

Success!

Help @bubbisthedog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.15,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...