简体   繁体   English

使用php中的会话登录

[英]login using session in php

<?php
ob_start();
include("db_connect.php");
$tbl_name='login';
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];
// 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);
$encrypted_mypassword = crypt($mypassword,'ctk'); 

 $sql="SELECT * FROM $tbl_name WHERE u_name='$myusername' and password='$encrypted_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){
session_register("myusername");
session_register("encrypted_mypassword");
header("location:edit-grid.php");
}
else {header("location:main_login.php?a=Login Failed Try Again!!");
//echo "Wrong Username or Password";
}
ob_end_flush();
?>

i have a login page shown above, in localhost it works fine,but shows an error in firebug: Failed to load source for: http://localhost/emp_tracker/main/checklogin.php 我有一个上面显示的登录页面,在localhost中工作正常,但在firebug中显示错误:无法加载源代码: http://localhost/emp_tracker/main/checklogin.php

In server that is when it is hosted it does not re-direct it fails(session fails), 在托管它的服务器中,它不会重定向它失败(会话失败),

Updated answer 更新的答案

This code should work: 这段代码应该有效:

session_start();
include("db_connect.php");

$myusername = mysql_real_escape_string($_POST['myusername']);
$mypassword = crypt($_POST['mypassword'], 'ctk'); 
$mypassword = mysql_real_escape_string($mypassword);

$sql = "SELECT * FROM `login` WHERE `u_name` = '$myusername' and `password` = '$encrypted_mypassword'";
$result = mysql_query($sql);

if(mysql_num_rows($result) == 1)
{
    $_SESSION['myusername'] = $myusername;
    $_SESSION['encrypted_mypassword'] = $encrypted_mypassword;
    header("Location: http://servername/folder/edit-grid.php");
}
else
{
    header("Location: http://servername/folder/main_login.php?a=Login Failed Try Again!!");
}

Original answer 原始答案

The line below doesn't make sense. 下面的一行没有意义。 You already started the session at the beginning of your script. 您已经在脚本的开头启动了会话。 Also session_start doesn't take any arguments. session_start也不接受任何参数。 Removing it may solve your problem. 删除它可以解决您的问题。

session_start('myusername');

Also, you should use the full URL when redirecting: 此外,您应该在重定向时使用完整的URL:

HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. HTTP / 1.1需要绝对URI作为»Location:包括方案,主机名和绝对路径的参数,但某些客户端接受相对URI。 You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself. 您通常可以使用$ _SERVER ['HTTP_HOST'],$ _SERVER ['PHP_SELF']和dirname()自己创建一个相对URI的绝对URI。

Source: http://www.php.net/manual/en/function.header.php 资料来源: http//www.php.net/manual/en/function.header.php

Edit: There is another mistake in your script. 编辑:您的脚本中还有另一个错误。 It's in this line: 它在这一行:

$_SESSION['myusername']=$result['myusername'];

You use $result['myusername']; 你使用$result['myusername']; , but you should fetch the results first. ,但你应该先获取结果。 Like this: 像这样:

$row = mysql_fetch_assoc($result);
$_SESSION['myusername'] = $row['u_name'];

Even i had the same issue, but could not figure out the actual reason. 即使我有同样的问题,但无法弄清楚实际的原因。 Some points which might help are, 1) There should be no output before header. 可能有帮助的一些要点是,1)标题之前应该没有输出。 ie. 即。 no echo or html 2) Also check if there is proper semicolon at end of each php statement wherever required. 没有echo或html 2)还要检查每个php语句的末尾是否有正确的分号。 3) After searching on google, it seems the issue might only show up in older version of firebug. 3)在google上搜索后,似乎问题可能只出现在旧版本的firebug中。 (Not sure) 4) Try running each line of code at a time. (不确定)4)尝试一次运行每行代码。 ie see if username and passowrd are posted properly by echoing. 即查看是否通过回显正确发布了用户名和passowrd。 Also database connection is proper and the query results are as expected. 数据库连接也是正确的,查询结果是预期的。

Do you get a 500 internal server error on server? 您是否在服务器上收到500内部服务器错误?

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

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