会话中的会话变量不会持续存在

时间:2013-05-10 22:30:08

标签: php

我正在创建一个需要将几个变量传递到新页面的会话。但是,当我设置任何等于$_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;

?> 

所以如果有人能给我任何帮助或者看错了什么,我真的很感激。

1 个答案:

答案 0 :(得分: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]与上述相同


除此之外代码运行应该......你究竟究竟是什么问题?

这是我测试的更正代码

FILE1.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'];

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