新会话登录脚本无法在PHP中运行

时间:2012-05-11 08:04:15

标签: php

我正在使用此脚本,请参阅下面的内容,我从此站点获取登录用户并根据他们的登录名重定向到网页。它工作得很好,但我无法将用户名拉入他们被重定向到的页面。下面的新会话脚本通常适用于简单的脚本,但它似乎不适用于此重定向脚本。如果有人能指出我正确的方向,我将非常感激,因为我是PHP的新手,这是我的第一次尝试。谢谢艾伦

重定向脚本效果很好。

<?php 
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="trydata"; // Database name 
$tbl_name="users"; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 
// username and password sent from form 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and   password='$mypassword'"; 
$result = mysql_query($sql); 


// Mysql_num_row is counting table row 
$count = mysql_num_rows($result); 

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count == 1){ 
    // Register $myusername, $mypassword and redirect to file"login_success.php" 
    $_SESSION['username'] = $myusername; 
    $_SESSION['password'] = $mypassword; 

    $result = mysql_fetch_array($result); // get the result set from the query

    $redirect = trim($result['redirect']); // get the redirect column's value

    if ($redirect == '') {
        echo "No redirect value was set!";
    } else {
        header('Location: ' . $redirect);
        exit;
    }
} else { 
    echo "Wrong Username or Password"; 
} 

?>

New session script not pulling loin information through.

<?php 
session_start(); // start up your PHP session! 


if (isset($_SESSION['username'])
{

    echo "You are logged in as ".$_SESSION['username'];
    echo "<p>";

    echo "<a href='logout.php'> Click here to logout<a/>";
    }

else

header ("Location: form.php");

?>

3 个答案:

答案 0 :(得分:0)

在重定向之前尝试使用session_write_close

if ($redirect == '') {
    echo "No redirect value was set!";
} else {
    session_write_close(); 
    header('Location: ' . $redirect);
    exit;
}

答案 1 :(得分:0)

确保设置session_start();在顶行,在<?php行之后访问$ _SESSION数组的脚本。

答案 2 :(得分:0)

<?php
session_start(); <----------------Session should start here 
$host="localhost"; // Host name 
$username=""; // Mysql username 
$password=""; // Mysql password 
$db_name="trydata"; // Database name 
$tbl_name="users"; // Table name

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 
// username and password sent from form 
$myusername=$_POST['username']; 
$mypassword=$_POST['password']; 

// To protect MySQL injection (more detail about MySQL injection) 
$myusername = stripslashes($myusername); 
$mypassword = stripslashes($mypassword); 
$myusername = mysql_real_escape_string($myusername); 
$mypassword = mysql_real_escape_string($mypassword); 

$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and   password='$mypassword'"; 
$result = mysql_query($sql); 


// Mysql_num_row is counting table row 
$count = mysql_num_rows($result); 

// If result matched $myusername and $mypassword, table row must be 1 row 
if($count == 1){ 
    // Register $myusername, $mypassword and redirect to file"login_success.php" 
    $_SESSION['username'] = $myusername; 
    $_SESSION['password'] = $mypassword; 

    $result = mysql_fetch_array($result); // get the result set from the query

    $redirect = trim($result['redirect']); // get the redirect column's value

    if ($redirect == '') {
        echo "No redirect value was set!";
    } else {
        header('Location: ' . $redirect);
        exit;
    }
} else { 
    echo "Wrong Username or Password"; 
} 

?>

New session script not pulling loin information through.

<?php 



if (isset($_SESSION['username'])
{

    echo "You are logged in as ".$_SESSION['username'];
    echo "<p>";

    echo "<a href='logout.php'> Click here to logout<a/>";
    }

else

header ("Location: form.php");

?>

您需要在一开始就致电session_start()

我建议你学习PDO