dongxing1965 2010-07-28 21:49
浏览 66

PHP会话变量 - 在Wit的结尾

I'm just getting in to MySQL and PHP--and I'm just trying to create a simple login system for a project we're testing. I've connected and created the login logic just fine, but now I can't for the life of me get the session variables to carry over to the new pages. Could someone please show me the correct way to do this?

Here is my login script--which is activated by submitting a form:

<?php 
session_start();

$link = mysql_connect('xxxxxxx.ipowermysql.com', 'xxxxxx', 'xxxxxx'); 
if (!$link) { 
    die('Could not connect: ' . mysql_error()); 
} 

mysql_select_db(austinhabich_IC_20090511_174535) or die(msql_error());

$email=$_POST['email']; 
$password=$_POST['password'];

$sql="SELECT * FROM player WHERE email='$email' and password='$password'";
$result=mysql_query($sql);

$count=mysql_num_rows($result);

if($count==1){

$_SESSION['status'] = "1";      

header("location: main.php");
}
else {
echo "Wrong Username or Password";
}
?>

And here is the page it redirects to:

 <?php session_start(); ?>
 ...doctype stuff...

 <html
 xmlns="http://www.w3.org/1999/xhtml">
 <head> <meta http-equiv="Content-Type"
 content="text/html; charset=utf-8" />
 <title>Untitled Document</title>
 </head>

 <body> 

 <?
 echo $_SESSION['status'];

 ?> </body> </html>

In this case, I'm just trying to even get the session variable to register, so I'm testing by attempting to print the variable's data. I've been trying to use isset and have it redirect back to the login page. The redirect worked, but it happened every time since the session variable is not registering.

PHP Verion is 5.2.12

  • 写回答

1条回答 默认 最新

  • dtra49684 2010-07-28 21:51
    关注

    On a quick glance, three things:

    • You seem to be missing session_start() in the first script.

    • You would get "Wrong username" if the account exists twice or more in the table, which can sometimes happen while testing.

    • You should die() after doing a header() redirect.

    • austinhabich_IC_20090511_174535 needs to be put into quotes.

    • session_start(); needs to be called in the head of the script, before any HTML is output.

    • Your SQL statements are vulnerable to SQL injection. Incoming data should urgently be sanitized using mysql_real_escape_string before used in a query

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大