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
[code=php]
<?php
header("Cache-Control: no-cache, must-revalidate");
?> [/code]
[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]
[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]
[code=php]
<?php
$_SESSION['name'] = "name";
...
$_SESSION['logged_in'] = true;
header("Location: someotherpage.php");
?>
[/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.[/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...[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]
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]...
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...
[CODE]
<?php
$_SESSION['name'] = "name";
...
$_SESSION['logged_in'] = true;
header("Location: someotherpage.php");
?>
[/CODE]
[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.[code=php]
$_SESSION['id'] = "nightshift"; with $_SESSION['id'] = $id;
[/code]
But only if you are using cookie-based sessions. [B]$_COOKIES != $_SESSIONS[/B]...[/QUOTE]
Which by default, is enabled in PHP.[/quote]... but can and does vary from server to server, hosting company to hosting company.
$_COOKIES != $_SESSIONSI was referring to this statement:
Originally Posted by chazzy
Also, I never said cookies are sessions.[/quote]
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.
0.1.9 — BETA 5.19