-2

我正在创建一个需要将多个变量传递到新页面的会话。但是,当我设置任何等于$_SESSION变量的值时,它会拒绝转移到新页面并回显。我的页面查看计数器继续记录刷新或查看的页面数。

<?php
session_name('work');
session_start();
$probt1 = 'probt1'; 
$_SESSION[probt3] = 'probt3';
$_SESSION['firstname'] = 'probt2';

if(isset($_SESSION['views']))
    $_SESSION['views']=$_SESSION['views']+1;
else
    $_SESSION['views']=1;
echo "Views=". $_SESSION['views'];
?>

第二页在这里。这是我试图从上面的代码中回显一些变量的地方。

<?php
session_name('work');
session_start();
$probt4 = 'probt4';
$_SESSION['firstname'];
$con=mysqli_connect("localhost", "root" , "******" , "******" );
if (mysqli_connect_errno($con))
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_SESSION['views']))
    $_SESSION['views']=$_SESSION['views']+1;
else
    $_SESSION['views']=1;
echo "Views=". $_SESSION['views'];

echo '<br/>'.$_SESSION['firstname'].'hello this is where stuff is echoed out'.     $probt1.   $_SESSION[probt3].$probt4;

?> 

因此,如果有人可以给我任何帮助或查看问题所在,我将不胜感激。

4

1 回答 1

0

你的代码一团糟。。

$_SESSION[probt3] = 'probt3';

应该是(引起注意)

$_SESSION['probt3'] = 'probt3';

这条线什么都不做

$_SESSION['firstname'];

echo '<br/>'.$_SESSION['firstname'].'hello this is where stuff is echoed out'.$probt1.   $_SESSION[probt3].$probt4;

$probt1未定义,$_SESSION[probt3]同上


除此之外,代码按应有的方式运行......你的问题到底是什么?

这是我测试的更正代码

文件1.php

//Global variable doesn't carry to the other page
$probt1 = 'probt1'; 

//Session variables do
$_SESSION['probt3'] = 'probt3';
$_SESSION['firstname'] = 'probt2';
$_SESSION['views'] = (isset($_SESSION['views'])) ? ++$_SESSION['views'] : $_SESSION['views'];

echo "Views = " . $_SESSION['views'];

文件2.php

<?php
session_name('work');
session_start();

$probt4 = 'probt4';

$_SESSION['firstname']; // This line isn't doing anything

$_SESSION['views'] = (isset($_SESSION['views'])) ? ++$_SESSION['views'] : $_SESSION['views'];

echo "Views=". $_SESSION['views'];

var_dump($_SESSION);
于 2013-05-10T22:57:44.957 回答