/    Sign up×
Community /Pin to ProfileBookmark

Mysql and php dynamic data refresh issue

Hi, I’m using mysql 4.0.27 with PHP 4.3.11 and my problem is:

I have a site that allows members to upload data to be used in their profiles.
The images and the MP3s are copied to the site file system(no Mysql).
The text data I store in Mysql then display where needed(forms, profile)

The problem is that the data coming out of Mysql does not does not show the changes that the user made until he/she shuts down their browser. Yet the image and MP3 files show their changes just by hitting refresh. Upon opening the browser and going back to site, all of the user’s changes from mysql now show. Only thing that I can find that will reflect changes is shutting down the browser.:eek:

I’ve tried setting a Random number variable to the url link to make the page a different name each time it is viewed, no dice.

I tried all of the PRAGMA NOCACHE methods.

Can anyone think of a reason why this might be a thorn in my side?

Bad PHP or HTML/CSS coding?
Bad Mysql settings?
Bad php.ini settings?
Could .htaccess fix?

I’ve run out of things to try to remedy this, please lend advice. thanks

to post a comment
PHP

25 Comments(s)

Copy linkTweet thisAlerts:
@chazzyJan 19.2007 — its your user's browser. they cache too much.
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 20.2007 — Actually, there are no real users yet(with browsers). The site is soon to launch and I am using IE, Firefox, and Netscape(all 3 default settings) to view my site and it's functions after I create them.

Most browsers by default are set to "cache the way they do by default" it seems. Which leaves me in a pickle. Cuz I can't ask folks to change their browser settings just for my site.

I think I have a code problem, just looking to see if anyone has ever run into this problem, then fixed it.

Thanks for the reply
Copy linkTweet thisAlerts:
@chazzyJan 21.2007 — do you have a cache header set?

what if you do something like this at the very top of your script?

[code=php]
<?php
header("Cache-Control: no-cache, must-revalidate");
?> [/code]


I'm also going to try to move this to the PHP forum, let me see if it works.?
Copy linkTweet thisAlerts:
@NightShift58Jan 21.2007 — Can you post some code?
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — Edit: I just logged in as a different user. When I relogged the original user, changes showed. So it seems if I kill their session, then reopen it immediately it would work? Anybody know how this is done?

thanks so much you guys for the replies, here's all the code:

I tried the 'no cache' method u gave, with no luck.

database records change, but won't show. If it helps to know, all of these items are declared in my login script. So they show, but just old stuff, until user closes browser, then reopens it. Then new stuff shows.

Question: Should I kill the user's session somehow, then reopen it? would that help, would it work?

form.php:
[CODE]
<?php
// At the very bottom I have the output fields for showing users their mysql data
session_start();
header("Cache-Control: no-cache, must-revalidate");
if (!$_SESSION['id']) {
echo "You're not logged in!";
include("doh.html");
exit();
}
?>
<html><head>
<title>Change Profile Info</title>
</head><body>
<h1>Change Profile Info</h1>
<p>Fill out only the fields you'd like to change:</p>




<form action="parse.php" method="post">


Artist Name: <input type="text" name="artname" size="20"><br />

Country: <input type="text" name="country" size="20"><br />

Province/State: <input type="text" name="province" size="20"><br />

city: <input type="text" name="city" size="20"><br />

Bio: <textarea rows="5" cols="40" name="bio"></textarea><br />

Influences: <textarea rows="5" cols="40" name="infl"></textarea><br />

<input type="submit" value="Update Profile" name="submit"><input type="reset" value="Reset Entries" name="reset"></form>
<br>
<br>
<br>
<?
print '<textarea rows="1" cols="40" name="bio" >' . $_SESSION['name'] . '</textarea><br>';
?>
<?
print '<textarea rows="1" cols="40" name="bio" >' . $_SESSION['country'] . '</textarea><br>';
?>
<?
print '<textarea rows="1" cols="40" name="bio" >' . $_SESSION['province'] . '</textarea><br>';
?>
<?
print '<textarea rows="1" cols="40" name="bio" >' . $_SESSION['city'] . '</textarea><br>';
?>
<?
print '<textarea rows="12" cols="40" name="bio" >' . $_SESSION['bio'] . '</textarea><br>';
?>
<?
print '<textarea rows="12" cols="40" name="bio" >' . $_SESSION['infl'] . '</textarea><br>';
?>

</body></html>
[/CODE]

parse.php:
[CODE]
<?php
// You'll see at bottom where I try to fix data problem
// with a random number variable, no luck again.

session_start();
header("Cache-Control: no-cache, must-revalidate");
if (!$_SESSION['id']) {
echo "You're not logged in!";
include("doh.html");
exit();
}

$id = $_SESSION['id'];
// Connect to Mysql
include ("mysql_conn.php");


if ($_POST['artname'] != "") {
$artname = htmlspecialchars($_POST['artname']);
mysql_query("UPDATE users SET name='$artname' WHERE id='$id'") or die (mysql_error());
$_SESSION['artname'] = $artname;
$cartname = "<li>artname</li>";
}
if ($_POST['country'] != "") {
$country = htmlspecialchars($_POST['country']);
mysql_query("UPDATE users SET country='$country' WHERE id='$id'") or die (mysql_error());
$_SESSION['country'] = $country;
$ccountry = "<li>country</li>";
}
if ($_POST['province'] != "") {
$province = htmlspecialchars($_POST['province']);
mysql_query("UPDATE users SET province='$province' WHERE id='$id'") or die (mysql_error());
$_SESSION['province'] = $province;
$cprovince = "<li>province</li>";
}
if ($_POST['city'] != "") {
$city = htmlspecialchars($_POST['city']);
mysql_query("UPDATE users SET city='$city' WHERE id='$id'") or die (mysql_error());
$_SESSION['city'] = $city;
$ccity = "<li>city</li>";
}
if ($_POST['bio'] != "") {
$bio = nl2br(htmlspecialchars($_POST['bio']));
mysql_query("UPDATE users SET bio='$bio' WHERE id='$id'") or die (mysql_error());
$_SESSION['bio'] = $bio;
$cbio = "<li>bio</li>";
}
if ($_POST['infl'] != "") {
$infl = nl2br(htmlspecialchars($_POST['infl']));
mysql_query("UPDATE users SET infl='$infl' WHERE id='$id'") or die (mysql_error());
$_SESSION['infl'] = $infl;
$cinfl = "<li>influences</li>";
}
?>
<html><head><title>Change Profile Results</title></head><body>
<h1>Change Profile Results:</h1>
<?
if (($cartname) || ($ccountry) || ($cprovince) || ($ccity) || ($cbio) || ($cinfl)) {
echo "The following items have been updated in your profile:<br /><ul>";
if ($cartname) {
echo $cartname;
}
if ($ccountry) {
echo $ccountry;
}
if ($cprovince) {
echo $cprovince;
}
if ($ccity) {
echo $ccity;
}
if ($cbio) {
echo $cbio;
}
if ($cinfl) {
echo $cinfl;
}
$str = '';
for ($i=1; $i<=10; $i++){
$set = array(rand (65,90),rand(97,122));
$str .= chr($set[rand(0,1)]);
}
echo "</ul><br />To go back to Info Management, <a href="form.php?var=<?$str?>">click here</a>.";
} else {


echo "Nothing has been changed<a href="form.php">Click here</a> To go back to Info Management.";
}
?>
</body></html>
[/CODE]
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — Just found this, so I'm gonna check it out, I think I can restart the session and it will behave as if the user closed their browser. but if you have help for me please give it.

http://us3.php.net/session_destroy

Yeah, move this thread if you can, it's all a php issue
Copy linkTweet thisAlerts:
@chazzyJan 21.2007 — Oh. No where did you mention you were using sessions until you posted code :-)

The problem is that session data doesn't actually become usable until after the page has been written to the browser. This is due to how cookies are handled in web apps. The way to work around this is to use redirection.

[code=php]
<?php
$_SESSION['name'] = "name";
...
$_SESSION['logged_in'] = true;

header("Location: someotherpage.php");
?>
[/code]


Then in someotherpage.php you should be able to use them properly. It works best via the MVC paradigm.
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — Sorry chazzy, I'm too new to all of this to get what you meant there entirely.

Are you relogging them in, to restart all data?

And is "someotherpage.php" just like my "form.php" that shows them their data at the bottom. That they were originally at to begin with?

Sorry, I'm very new, please bear with me?

thanks

I know nothing about cookies, in all my work so far I have not had to implement the use of them.
Copy linkTweet thisAlerts:
@chazzyJan 21.2007 — actually, you've been using cookies the whole time if you started using sessions.

as for what i mean, i mean you should be using 2 pages, minimum. they can be all in the same file, but you should logically separate your display code from your saving code.

Basically what I mean is that in parse.php, you have a clear separation of the logic. The top of your script runs fine, and it sets session variables. However, since the page hasn't completely executed the session data hasn't been written so it can't access it on that page. You need to make the lower portion part of a redirect, so that the session data can be written when you change the location via header like I showed you in my last post. In the page you redirect to, it should have similar code that that HTML section of your page. Note that you might have to change a few things based on how your code is done.
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — Oh no, that parse.php only lists the items changed to the user on that page. It does not, and is not intended to display the actual contents of the database field.(form.php is for that, showing actual data from mysql)

In form.php when they go back through the link at bottom of parse.php, is where I'm displaying the changes, but they don't reflect the new info in my database.

I think maybe you think I'm trying to display the mysql data in 'parse.php'

But I'm not. I display it in 'form.php'

You'll see in the top portion of parse.php that I render an html list based upon the user's changes(to show them what they changed, not the change itself), that's all I'm displaying in parse.php. Not the data. Check the bottom of my 'form.php' to see where I display the Mysql data.

Thanks for moving this chazzy!
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — Does anyone know how I could use:

"session_destroy", or

"session_unset", or

"session_unregister"

To unset the variables, but keep the user's login variables going?

Or re-identify the user somehow after I destroy their session in the script somewhere?

If I could just simulate the user log-out, then log them back in, it would work. I need a way to log users in without them doing it themselves, if that is possible, this would all work.

Is there a way to unset just certain variables, then reset them based on the user's "id"?


Does that make sense?
Copy linkTweet thisAlerts:
@NightShift58Jan 21.2007 — The problem is that session data doesn't actually become usable until after the page has been written to the browser. This is due to how cookies are handled in web apps.[/quote]Careful... this is true for cookies but NOT sessions. Test this:[code=php]<?php
session_start();
$_SESSION['count'] = 0;

FOR ($x=0; $x<100; $x++) :
$_SESSION['count']++;
ENDFOR;

print $_SESSION['count'];
?>[/code]
If your theory was correct, I couldn't define, increment and display the new value of $_SESSION['count'] in the same script/page. If that were the case, many session-based login system wouldn't work...
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — What if I create a function that will restart the user's session in the top portion of the script, then after I destroy the session, re-identify the user through the newly made function? That should assure everything is dumped.

Could you help me create such a function?
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — I tried this from php.net, but I'm not sure what I should change about it to work for me! Help!

The output I got was a jumbled line of like 15 characters, and a message that I was not logged in. Which means it killed the session and vars, but how do I re-identify the user?

[CODE]
<?php

session_start();

// Some simple code etc etc
$requested_logout = true;

if ($requested_logout) {
session_restart();
}

// Now the session_id will be different every browser refresh
print(session_id());

function session_restart()
{
if (session_name()=='') {
// Session not started yet
session_start();
}
else {
// Session was started, so destroy
session_destroy();

// But we do want a session started for the next request
session_start();
session_regenerate_id();

// PHP < 4.3.3, since it does not put
setcookie(session_name(), session_id());
}
}

?>

[/CODE]
Copy linkTweet thisAlerts:
@NightShift58Jan 21.2007 — actually, you've been using cookies the whole time if you started using sessions.[/quote]But only if you are using cookie-based sessions. [B]$_COOKIES != $_SESSIONS[/B]...
Copy linkTweet thisAlerts:
@NightShift58Jan 21.2007 — What if I create a function that will restart the user's session in the top portion of the script, then after I destroy the session, re-identify the user through the newly made function? That should assure everything is dumped.[/QUOTE]This is likely to be counterproductive...
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — thanks nightshift, what would you do in my place?
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — I'm gonna give this a shot, even though I don't really understand what each line is doing? Does it just overwrite their old session instantly?
[CODE]
<?php
$_SESSION['name'] = "name";
...
$_SESSION['logged_in'] = true;

header("Location: someotherpage.php");
?>
[/CODE]
Copy linkTweet thisAlerts:
@NightShift58Jan 21.2007 — See a working example at: http://www.nightshift58.com/webdev/test_session_scope/form.php

I've made some changes to form.php:

1.[code=php]if (!$_SESSION['id']) { [/code]to:[code=php] if (!isset($_SESSION['id'])) {[/code]It now looks like this:[code=php]<?php
// At the very bottom I have the output fields for showing users their mysql data
session_start();
header("Cache-Control: no-cache, must-revalidate");
if (!isset($_SESSION['id'])) {
// hard coded for testing...
$_SESSION['id'] = "nightshift";
//echo "You're not logged in!";
//include("doh.html");
//exit();
}
?>
<html><head>
<title>Change Profile Info</title>
</head><body>
<h1>Change Profile Info</h1>
<p>Fill out only the fields you'd like to change:</p>

<form action="parse.php" method="post" >
Artist Name: <input type="text" name="artname" size="20"><br />
Country: <input type="text" name="country" size="20"><br />
Province/State: <input type="text" name="province" size="20"><br />
city: <input type="text" name="city" size="20"><br />
Bio: <textarea rows="5" cols="40" name="bio"></textarea><br />
Influences: <textarea rows="5" cols="40" name="infl"></textarea><br />
<input type="submit" value="Update Profile" name="submit">
<input type="reset" value="Reset Entries" name="reset">
</form>
<br>
<?
print '<textarea rows="1" cols="40" name="bio" >' . $_SESSION['artname'] . '</textarea><br>';
print '<textarea rows="1" cols="40" name="bio" >' . $_SESSION['country'] . '</textarea><br>';
print '<textarea rows="1" cols="40" name="bio" >' . $_SESSION['province'] . '</textarea><br>';
print '<textarea rows="1" cols="40" name="bio" >' . $_SESSION['city'] . '</textarea><br>';
print '<textarea rows="12" cols="40" name="bio" >' . $_SESSION['bio'] . '</textarea><br>';
print '<textarea rows="12" cols="40" name="bio" >' . $_SESSION['infl'] . '</textarea><br>';
?>

</body></html>[/code]
I also made minor changes to parse.php. Basically changed the way you were updating the data. Instead of performing one update per changed field, I do one at the end for all changed fields. It now looks like this:[code=php]<?php
// You'll see at bottom where I try to fix data problem
// with a random number variable, no luck again.

session_start();
header("Cache-Control: no-cache, must-revalidate");
if (!isset($_SESSION['id'])) {
// hard coded for testing...
$_SESSION['id'] = "nightshift";
//echo "You're not logged in!";
//include("doh.html");
//exit();
}

$id = $_SESSION['id'];
// Connect to Mysql
//include ("mysql_conn.php");
include ("../DBconnect.inc.php");

$arrSQL = array();
if ($_POST['artname'] != "") {
$artname = htmlspecialchars($_POST['artname']);
$arrSQL[] = "name='" . mysql_escape_string($artname) . "'";
$_SESSION['artname'] = $artname;
$cartname = "<li>artname</li>";
}
if ($_POST['country'] != "") {
$country = htmlspecialchars($_POST['country']);
$arrSQL[] = "country='" . mysql_escape_string($country) . "'";
$_SESSION['country'] = $country;
$ccountry = "<li>country</li>";
}
if ($_POST['province'] != "") {
$province = htmlspecialchars($_POST['province']);
$arrSQL[] = "province='" . mysql_escape_string($province) . "'";
$_SESSION['province'] = $province;
$cprovince = "<li>province</li>";
}
if ($_POST['city'] != "") {
$city = htmlspecialchars($_POST['city']);
$arrSQL[] = "city='" . mysql_escape_string($city) . "'";
$_SESSION['city'] = $city;
$ccity = "<li>city</li>";
}
if ($_POST['bio'] != "") {
$bio = nl2br(htmlspecialchars($_POST['bio']));
$arrSQL[] = "bio='" . mysql_escape_string($bio) . "'";
$_SESSION['bio'] = $bio;
$cbio = "<li>bio</li>";
}
if ($_POST['infl'] != "") {
$infl = nl2br(htmlspecialchars($_POST['infl']));
$arrSQL[] = "infl='" . mysql_escape_string($infl) . "'";
$_SESSION['infl'] = $infl;
$cinfl = "<li>influences</li>";
}
if ($arrSQL) {
// We do it this way to only perform a single update instead of "n" updates
$sql_sets = implode(", ", $arrSQL);
$sql = "UPDATE users SET " . $sql_sets . " WHERE id='$id' LIMIT 1 ";
print "<hr>" . $sql . "<hr>";
$qry = mysql_query($sql) or die("SQL Error: $sql<br>" . mysql_error());
?>
<html><head><title>Change Profile Results</title></head><body>
<h1>Change Profile Results:</h1>
<?
echo "The following items have been updated in your profile:<br /><ul>";
if ($cartname) {
echo $cartname;
}
if ($ccountry) {
echo $ccountry;
}
if ($cprovince) {
echo $cprovince;
}
if ($ccity) {
echo $ccity;
}
if ($cbio) {
echo $cbio;
}
if ($cinfl) {
echo $cinfl;
}
echo "</ul><br />To go back to Info Management, <a href='form.php'>click here</a>.";
} else {
echo "Nothing has been changed<a href="form.php">Click here</a> To go back to Info Management.";
}
?>
</body></html>[/code]
As you can see, no need to destroy sessions or close the browser. If this had to be done, no session-based login system would work.
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — Thanks for tidying up my code nightshift, but my problem still persists.

let me get this right, I was supposed to change in both files:
[code=php]
$_SESSION['id'] = "nightshift"; with $_SESSION['id'] = $id;
[/code]

right?

It does exactly like it did before, no change of the data back on "form.php" upon return from "parse.php" because we're under the same session as we were when we first visited form.php. I need a way to change the session or redo it to release all of the global variables. The MySQL data did change like before also using your modified files. I see the change in database, but not back on the form.php when I am sent there from parse.php
Copy linkTweet thisAlerts:
@NightShift58Jan 21.2007 — As you saw on my test script, it remembers the session data, back and forth.

So, this means that the basic mechanism works. What's the difference between you script and my test? In mine, the session id was hard coded. This can only mean that there is a problem in the script PRIOR to form.php - I can only think that it isn't assigning the session variable at all or incorrectly.

Can you post that one?
Copy linkTweet thisAlerts:
@WebjedikungfuauthorJan 21.2007 — NightShift, I love you man.:p You are amazing.?

First let me say sorry for doubting your code.

I had a couple variables wrong when I tested your code and all throughout my code attempts.

Then went back to your live example, saw it work so well, and then dug back in.

Fixed my bad variables, set it all up, and it works great.?

The way you cleaned my code is amazing!

I love it, and it works awesome! I just listed you as one of my Influences in the form.php? I wish I could borrow your brains for about a lifetime.

So does the form and parse look pretty secure to you(security wise)?
Copy linkTweet thisAlerts:
@NightShift58Jan 21.2007 — I have to admit, it's one of the few posts where my typing mistakes were down to a minimum....

Thanks for the kind words.

The only truly secure form is letter size, folded in four and stuck deep in your pocket!

You'll want to change all the mysql_escape_string() to mysql_real_escape_string() - I used that because when I started testing, I was doing without the database and then forgot to change it. You may also want to check $_SERVER['HTTP_REFERER'] in parse.php, to make sure that the POST is coming from the right place.
Copy linkTweet thisAlerts:
@chazzyJan 22.2007 — But only if you are using cookie-based sessions. [B]$_COOKIES != $_SESSIONS[/B]...[/QUOTE]

Which by default, is enabled in PHP.

http://www.php.net/manual/en/ref.session.php

Also, I never said cookies are sessions.
Copy linkTweet thisAlerts:
@NightShift58Jan 22.2007 — Which by default, is enabled in PHP.[/quote]... but can and does vary from server to server, hosting company to hosting company.$_COOKIES != $_SESSIONS

Originally Posted by chazzy

Also, I never said cookies are sessions.[/quote]
I was referring to this statement:The problem is that session data doesn't actually become usable until after the page has been written to the browser. This is due to how cookies are handled in web apps. The way to work around this is to use redirection.[/quote]This is true, of course, for cookies but quite false for sessions, where session variables, unlike cookies, are immediately available upon declaration/definition.

I didn't mean to imply that you didn't know the difference. I only wanted to point out to the OP that PHP handles sessions and cookies in a very different manner, and, by extension, that a redirect, which may be needed with cookies, was not necessary when using sessions.
×

Success!

Help @Webjedikungfu 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.19,
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,
)...