1

我正在使用这个脚本,见下文,我从这个站点获得了登录用户并根据他们的登录重定向到网页。它工作得很好,但我无法将用户名拉到他们重定向到的页面中。下面的新会话脚本通常适用于简单脚本,但似乎不适用于此重定向脚本。如果有人能指出我正确的方向,我将不胜感激,因为我对 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");

?>
4

3 回答 3

0

在重定向之前尝试使用session_write_close :

if ($redirect == '') {
    echo "No redirect value was set!";
} else {
    session_write_close(); 
    header('Location: ' . $redirect);
    exit;
}
于 2012-05-11T08:06:21.487 回答
0

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

于 2012-05-11T08:06:37.467 回答
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

于 2012-05-11T08:11:38.583 回答