FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


Forum Redirection Question





ddukki
I'm trying to get my forums to redirect back to the posted reply after someone replies to a topic.

The current coding inserts the value into the mysql table, then sets a five second cookie with the value of the topic ID and then redirects:

Code:

$addpost_query = "INSERT INTO posts
(poster_id,topic_id,post_entry,post_date,board_id)
VALUES
('".$_SESSION['fkumc_u_id']."','".$topic_id."','".$post."',now(),'".$board_id."')";

mysql_query($addpost_query) or die(mysql_error());

setcookie('fkumc_reply_topic', $topic_id ,time()+5);
setcookie('fkumc_reply_post', $post_num ,time()+5);
header ("Location: /FKUMC/redirect.php");}}


The Redirect page then is supposed to take the values of the topic and post ID's and use them to direct to the said topic. The problem is that the redirect page fails to read the cookies at all and just goes to the default home page which it's supposed to do if the $_COOKIE['fkumc_reply_topic'] isn't set (which is set by the reply function). Is there a reason that the page can't read the cookie?

Also, if there is a better way to do this, please tell me, because I'm thinking my coding is over-complicated.
hexkid
Maybe there's a time difference between the server and the client that isn't taken into account with the cookies ???

Suppose the server is late by 10 seconds ... the cookies it sends expire on 9:42:42 pm but the time on the client is 9:42:47 so the cookie doesn't get set.


I'd do the passing of data with session variables.

There are a couple problems with cookies:
a) the client may have them disabled
b) they are not trustworthy (can be changed/deleted/created by the client)

You're already using session variables, so just add the topic and post ID to the $_SESSION array, fetch and delete them on the redirect.php page.
Code:
<?php // your script which inserts a new post
// setcookie(...);
$_SESSION['last_post'] = array('topic'=>$topic_id, 'post_id'=>$post_num);
header('Location: /FKUMC/redirect.php');
exit('Redirected <a href="/FKUMC/redirect.php">here</a>.');
?>
Code:
<?php // redirect.php
if (isset($_SESSION['last_post'])) {
  $last_post_topic = $_SESSION['last_post']['topic'];
  $last_post_id = $_SESSION['last_post']['post_id'];
  unset($_SESSION['last_post']);
}
ddukki
It's still not reading the Session variable. Do the session and cookies have to be saved in a certain path, or does it not matter? My redirect page is in a different folder than the forums.

My logon page which uses cookies works just fine. The time is time()+5 ...
hexkid
ddukki wrote:
It's still not reading the Session variable. Do the session and cookies have to be saved in a certain path, or does it not matter? My redirect page is in a different folder than the forums.

My logon page which uses cookies works just fine. The time is time()+5 ...
Well, I guess that means the possible time difference between client and server isn't the culprit for cookies not working. I'm out of ideas ...

The folder things are on should not matter for the correct functioning of session variables. But you need to start the session in *every* page that uses session variables.


Does this script "work"?
Code:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');

session_start();

$ccount = 0;
if (isset($_COOKIE['count'])) $ccount = $_COOKIE['count'];

$scount = 0;
if (isset($_SESSION['count'])) $scount = $_SESSION['count'];
else $_SESSION['count'] = 0;

$ccount++;
$scount++;

setcookie('count', $ccount);
$_SESSION['count'] = $scount;

echo 'cookie count: ', $ccount, "<br>\n";
echo 'session count: ', $scount, "<br>\n";
echo "<br>\n";
echo 'Please <a href="">refresh this page</a>. Thank you :)'
?>
ddukki
I looked through the manual for cookies and I realized that the directory path needs to be included in the setcookie() function. Same with the sessions, but thanks for the advice.
Related topics
How do i sign up
How to write a code which will keep bandwidth to a minimum?
A ask a question forum
Problem with phpbb forum
MySQL
Forum Question
Web Site Forum Related Question
What can you do with Frih$
Emoticon-Poll
Do you think creationism should be regarded as science?
Young Programmers apparently ignored? :(
Faith forum?
Banned from sex
Forum arrangement question...
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.