CyberShot Posted August 15, 2009 Share Posted August 15, 2009 I have this script for a web counter. I did not write it, I followed a tutorial. I am trying to learn how to use the information in another script. The idea is to have the web counter on a page but send the results to a different page. I have already set up a method of doing this by just reading the file again on the page I want the results. But I thought there might be a better way. Some say use sessions, But I have never done this and when I tried, it didn't work for me. Here is the code I have now. I want to send $fstring to another page $filename= "reader.txt"; $fd = fopen ($filename , "r") or die ("Can't open $filename"); $fstring = fread ($fd , filesize ($filename)); fclose($fd); $fd = fopen ($filename , "w") or die ("Can't open $filename"); $fcounted = $fstring + 1; $fout= fwrite ($fd , $fcounted ); fclose($fd); Quote Link to comment https://forums.phpfreaks.com/topic/170367-sending-variables-to-another-page/ Share on other sites More sharing options...
oni-kun Posted August 15, 2009 Share Posted August 15, 2009 You can use $_POST or $_GET to send hidden variables to another page, $_GET will be visible in the url though.. For example these two pages. <?php //form.php $var = "Hey!"; header('Location: ./test.php?variable=$var'); ?> <?php //test.php $passedvar = $_GET['variable']; echo $passedvar; ?> Quote Link to comment https://forums.phpfreaks.com/topic/170367-sending-variables-to-another-page/#findComment-898708 Share on other sites More sharing options...
CyberShot Posted August 15, 2009 Author Share Posted August 15, 2009 interesting. i will try that right now. Quote Link to comment https://forums.phpfreaks.com/topic/170367-sending-variables-to-another-page/#findComment-898709 Share on other sites More sharing options...
CyberShot Posted August 15, 2009 Author Share Posted August 15, 2009 I get an error that says Cannot modify header information - headers already sent by (output started at /counter/index.php:9) in counter/counter.php on line 10 which is this header("Location: ./display/index.php$variable=$fstring"); Quote Link to comment https://forums.phpfreaks.com/topic/170367-sending-variables-to-another-page/#findComment-898711 Share on other sites More sharing options...
oni-kun Posted August 15, 2009 Share Posted August 15, 2009 Place ob_start() at the beginning of your php code.. <?php ob_start() {your code} header(...) ?> Quote Link to comment https://forums.phpfreaks.com/topic/170367-sending-variables-to-another-page/#findComment-898712 Share on other sites More sharing options...
CyberShot Posted August 15, 2009 Author Share Posted August 15, 2009 I still get the same error Quote Link to comment https://forums.phpfreaks.com/topic/170367-sending-variables-to-another-page/#findComment-898714 Share on other sites More sharing options...
DarkendSoul Posted August 15, 2009 Share Posted August 15, 2009 If you're including you should be able just set a variable before the include and call it in the next file. You could also use sessions. http://ca.php.net/manual/en/book.session.php Quote Link to comment https://forums.phpfreaks.com/topic/170367-sending-variables-to-another-page/#findComment-898716 Share on other sites More sharing options...
CyberShot Posted August 15, 2009 Author Share Posted August 15, 2009 ok, I got the session_start to work, but it echoes $fstring to the screen instead of the number of hits. I have an index page that I have incuded page one into and I put the session start there. Now I am not getting any errors, I am just not getting the value of the variable. page 1 <?php $filename= "reader.txt"; $fd = fopen ($filename , "r") or die ("Can't open $filename"); $fstring = fread ($fd , filesize ($filename)); $_SESSION['pageHits'] = '$fstring'; fclose($fd); $fd = fopen ($filename , "w") or die ("Can't open $filename"); $fcounted = $fstring + 1; $fout= fwrite ($fd , $fcounted ); fclose($fd); ?> page 2 <?php session_start(); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Untitled Document</title> </head> <body> <?php echo $_SESSION['pageHits']; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/170367-sending-variables-to-another-page/#findComment-898721 Share on other sites More sharing options...
DarkendSoul Posted August 15, 2009 Share Posted August 15, 2009 $_SESSION['pageHits'] = $fstring; That should fix things. Quote Link to comment https://forums.phpfreaks.com/topic/170367-sending-variables-to-another-page/#findComment-898725 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.