简体   繁体   English

会话无法正常工作

[英]session doesn't work properly

i having problem with sessions in localhost, the problem is the session doesn't work actually when it redirects to new post page it response's as you should login first 我在localhost中的会话有问题,问题是当会话重定向到新的帖子页面时,会话实际上不起作用,因为您应该先登录

Login code 登录码

<?php
include '../Class/db.class.php';
include '../Class/validation.class.php';
@session_start();
if(!$_SESSION['user']){
    $link=$_GET['dir'];
    if($link){
echo <<<EOF
<html> <head><title>ورود</title></head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<form method='post' action='index.php?dir=$link'>
  Username: <input type='text' name='user'><br />
 password<input type='password' name='pass'><br />
  <input type='hidden' name='hidd' value=1>
    <input type='submit' value='login'><br />
</form>
EOF;
    }
 else {
    echo <<<EOF
<html> <head><title>login</title></head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<form method='post' action='index.php'>
   username: <input type='text' name='user'><br />
 password: <input type='password' name='pass'><br />
  <input type='hidden' name='hidd' value=1>
    <input type='submit' value='login'><br />
</form>
EOF;
    }
}
$db=new db("localhost", "root", "", "blog");
$con=$db->connect();
$logout=$_GET['logout'];
$user=$_POST['user'];
$pass=$_POST['pass'];
$hidden=$_POST['hidd'];
if($hidden){
if(!$user){
    echo "username field can't left blank";
}
 elseif (!$pass) {
    echo 'password field can't left blank!';

}
else{
    $userres=$db->usersearch($user, $pass);

    if($userres==FALSE){
        echo 'username or password is wrong';
    }
 else {
       $_SESSION['user']=$user;

    }
}
}
if($_SESSION['user']){
  $link=$_GET['dir'];


echo "you logged in ".$_SESSION['user']; 
if($link=="new")
    echo  '<meta http-equiv="REFRESH" content="0;url=../admin/post/new.php">';

}

if($logout)
    session_destroy();
if($_SESSION['user'])
echo '<br /><a href="?logout=1">logout</a>';

?>

new post code 新邮递区号

<?php
@session_start();

if($_SESSION['user']=="admin"){
   include '../../Class/db.class.php';
echo <<<EOF
<form method=post action="new.php">
 <br />   subject:
    <br />
    <input type="text" name="title"><br />
   post:<br />

   <textarea cols=50 rows=10 name="post"></textarea><br />
    <input type="submit" value="send">

EOF;
$title=$_POST['title'];
$post=$_POST['post'];
$db=new db("localhost", "root", "", "blog");
$db->connect();
if($title && $post){

   $d=$db->post_insert($title, $title, 1);
    if($d==TRUE)
            echo "post created";
    else
        echo "an error occurred while sending the post";




}

$dd=$db->read(0, 20);
for($i=0;$i<20;$i++){
   echo $dd['content'][$i]."<br />";
}

}
else
{
    echo "you should login first";
    echo $_SESSION['user'];
}
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>

please help me! 请帮我!

I found others answers but don't solved my problem. 我找到了其他答案,但没有解决我的问题。

I have a login page.. it's Works well. 我有一个登录页面..很好。

Now I want that user choice your location in index.html.. then the next page the ads will show only images comes from your location. 现在,我希望该用户在index.html ..中选择您的位置。然后广告将仅显示图像的下一页来自您的位置。

It's ok but when users visit others links and come back do main page, session has destroyed 可以,但是当用户访问其他链接并返回主页时,会话已被破坏

I use: 我用:

INDEX.HTML

<form ... ok>
<input name="state">
<input type="submit">
</form>


MAINPAGE.PHP

<?php 
session_start();
$_SESSION['local'] = $_POST['estado'];
$_SESSION['local'];
 ?>
 <!--here is ok-->

 OTHER PAGE
 <?php 
session_start();
$_SESSION['local'];
 ?>
 <!--here is ok too.. session keeps alive but when I back with a menu link session is ended -->

The fact you're suppressing error output from session_start() with the @ symbol suggests that session_start is reporting errors that you're ignoring. 您使用@符号抑制了来自session_start()的错误输出的事实表明,session_start正在报告您所忽略的错误。

If it's reporting errors, you should look at the error it's generating to determine what's wrong. 如果报告错误,则应查看它所生成的错误,以确定出了什么问题。

Usually, session_start fails due to headers having already been sent. 通常,由于已经发送了头,所以session_start失败。 This happens if your script outputs any text before session_start is called. 如果您的脚本在调用session_start之前输出任何文本,则会发生这种情况。 Once headers have been sent, the session cannot be started as it depends on sending headers. 发送标头后,会话将无法启动,因为它取决于发送标头。

The fix for this is to make sure that session_start is the first thing to happen in your script. 解决此问题的方法是确保session_start是脚本中首先发生的事情。 As session_start happens as the first thing after your includes, then this means there's either whitespace at the start of your script, or whitespace or statements that generate output in the scripts you include. 由于session_start作为包含对象之后的第一件事发生,因此这意味着脚本开始处有空格,或者在包含的脚本中生成输出的空格或语句。 You need to check the included files and fix any white space (anything that's outside of the PHP tags) and check that they don't output anything until the session_start occurs. 您需要检查包含的文件并修复任何空格(PHP标记之外的任何空格),并检查它们在session_start发生之前没有输出任何内容。

On a side note, never use @ for error suppression. 附带说明,切勿使用@来抑制错误。 It's the coding equivalent of sweeping potentially serious problems that need to be addressed under the carpet. 这相当于在地毯下需要解决的潜在的严重问题的编码等效项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM