Jump to content

sending variables to another page


CyberShot

Recommended Posts

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);

Link to comment
https://forums.phpfreaks.com/topic/170367-sending-variables-to-another-page/
Share on other sites

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;
?>

 

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.