简体   繁体   English

如何获得 session id

[英]how to get the session id

please help i have the following php code for my login session but i am trying to get the $_session['user_id'] instead of the $_session['email'].请帮助我有以下 php 代码用于我的登录 session 但我试图获取 $_session ['user_id'] 而不是 $_session ['email']。 i tried print_f function to see what i can use but user_id array says 0 which cannot be right unless i read it wrong.我尝试使用 print_f function 来查看我可以使用什么,但 user_id 数组显示 0 除非我读错了否则这是正确的。

session_start();

$email = strip_tags($_POST['login']);
$pass = strip_tags($_POST['password']);

if ($email&&$password) {
    $connect = mysql_connect("xammp","root"," ") or die (" ");
    mysql_select_db("dbrun") or die ("db not found");
    $query = mysql_query("SELECT email,pass FROM members WHERE login='$email'");
    $numrows = mysql_num_rows($query);
    if ($numrows!=0) {
        // login code password check
        while ($row = mysql_fetch_assoc($query)) {
            $dbemail = $row['login'];
            $dbpass = $row['password'];
        }

        // check to see if they match!
        if ($login==$dbemail&&$password==$dbpass) {
            echo "welcome <a href='member.php'>click to enter</a>";
            $_SESSION['login']=$email;
        } else {
            echo (login_fail.php);
        }
    } else {
        die ("user don't exist!");
    }
    //use if needed ==> echo $numrows;
} else {
    die ("Please enter a valid login");
}

i am trying to get the $_session['user_id'] instead how can get this to use instead of $_session['email'].我正在尝试获取 $_session['user_id'] 而不是如何使用它而不是 $_session['email']。 tried using $_session['user_id'] but instead i got undefined error msg.尝试使用 $_session['user_id'] 但我得到了未定义的错误消息。

Well, you don't define $_session['user_id'] anywhere in this script, so it's no surprise that it's not defined.好吧,你没有在这个脚本的任何地方定义 $_session['user_id'],所以它没有定义也就不足为奇了。 You have to assign it a value before you can refer to it.您必须先为其分配一个值,然后才能引用它。

Also, note that there all kinds of security problems with this code.另外,请注意,此代码存在各种安全问题。

You're running your MySQL connection as the root user.您正在以 root 用户身份运行 MySQL 连接。 This is NOT a good idea.这不是一个好主意。

You're trusting user input, which opens your script up to a SQL injection attack.您信任用户输入,这会将您的脚本打开到 SQL 注入攻击。 Stripping HTML tags from the user input does not make it safe.从用户输入中去除 HTML 标签并不能保证安全。 Suppose that I came to your site, and filled in the "email" field with this:假设我来到您的网站,并在“电子邮件”字段中填写以下内容:

bob@example.com'; GRANT ALL PRIVILEGES ON *.* TO 'evil_bob' IDENTIFIED BY '0wned_joo';

As currently written, your script would happily run its query as normal, and also create an account called "evil_bob" with full privileges to all the information in all of the databases on your server.正如目前所写的那样,您的脚本将愉快地正常运行其查询,并创建一个名为“evil_bob”的帐户,该帐户对您服务器上所有数据库中的所有信息具有完全权限。

To avoid this, NEVER assume that user input is safe.为避免这种情况,切勿假设用户输入是安全的。 Validate it.验证它。 And to be extra sure, don't stick variables straight into SQL you've written.并且要更加确定,不要将变量直接粘贴到您编写的 SQL 中。 Use bound parameters instead.请改用绑定参数。 There are a few cases where it's hard to avoid -- for example, if you need to specify the name of a column rather than a piece of data, a bound parameter will not help and you'll have to do it some other way.有一些情况是很难避免的——例如,如果您需要指定列的名称而不是一条数据的名称,绑定参数将无济于事,您必须以其他方式进行。 However, for any piece of data you're using as part of a query, bind it.但是,对于您在查询中使用的任何数据,请绑定它。

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

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