doujishao8793 2012-05-11 08:04
浏览 24

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

I am using this script see below, which I got from this site to login users and redirect to a webpage based on their login. It works great but I am having trouble pulling the username into the page they are redirected to. The new session script below usually works with simple scripts but it does not appear to work with this redirect script. If someone could point me in the right direction I would be grateful as I am very new to PHP, this is my first attempt. Thanks Alan

Redirect script works great.

<?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条回答 默认 最新

  • doutale7115 2012-05-11 08:06
    关注

    try using session_write_close just before redirecting:

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

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?