我有两种类型的用户,一种是"付费"和另一个"未支付"。我想限制未付费用户查看view_profile.php页面

时间:2016-03-16 14:24:28

标签: php

我希望限制未付款但在用户登录后查看view_profile.php页面。只有付费用户才能访问该页面。登录后,两个用户会话都将启动,但只有付费用户才能看到view_profile.php页面。未付费用户将被重定向到其他页面。我的login.php文件代码是。

<?php
$username= mysql_real_escape_string($_POST['email']);
$password= mysql_real_escape_string($_POST['pass']);

$login= mysql_real_escape_string($_POST['login_user']);

if(isset($login)){
  $mysqli = new mysqli("localhost","username","password","database");           
  if ($mysqli->connect_errno) {
    echo "Failed to connect to MySQL: " . $mysqli->connect_error;
  }
  $res = $mysqli->query("SELECT * FROM users where email='$username' and password='$password'");
  $row = $res->fetch_assoc();

  $name = $row['first_name'];
  $user = $row['email'];
  $pass = $row['password'];
  $type = $row['status'];
  if($user==$username && $pass=$password){
    session_start();
    if($type=="Paid"){
      $_SESSION['mysesi']=$name;
      $_SESSION['mytype']=$type;
      echo "<script>alert('Loged in successfully !')</script>";
      echo "<script>window.location.assign('view_profile.php')</script>";
    } else if($type=="Unpaid"){
      $_SESSION['mysesi']=$name;
      $_SESSION['mytype']=$type;
      echo "<script>window.location.assign('index.php')</script>";
    } 

    else{

        echo "<script>alert('Wrong username or password')</script>";
                    echo "<script>window.open('login.php?not_admin=Check%20your%20Email%20and%20Password%20otherwise %20You%20are%20not%20an%20Registred%20User%20!','_self')</script>";
    }

  }
}  
?>

view_profile.php页面顶部代码是..只有付费用户才能看到这个页面..无偿用户即使他已经插入但他也不会看到view_profile.php页面..

<?php
//connect database
$con = mysqli_connect ("localhost","username","password","database");           
        //database connect error
        if (mysqli_connect_errno())
        {   
        echo "Failed to connect to MySql: " . mysqli_connect_errno();
        }

session_start(); // Use session variable on this page. This function must put on the top of page.

if (!isset($_SESSION['mysesi']) && !isset($_SESSION['mytype'])=='Paid')
{
  echo "<script>window.location.assign('login.php')</script>";
}



?>

index.php页面代码是......

<?php
//connect database
$con = mysqli_connect ("localhost","username","password","database");           
        //database connect error
        if (mysqli_connect_errno())
        {   
        echo "Failed to connect to MySql: " . mysqli_connect_errno();
        }

session_start(); // Use session variable on this page. This function must put on the top of page.

if (!isset($_SESSION['mysesi']) && !isset($_SESSION['mytype'])=='Unpaid')
{
  echo "<script>window.location.assign('login.php')</script>";
}



?>

1 个答案:

答案 0 :(得分:3)

这个答案是概述您正在进行的语法错误,并且对于评论来说太长了。您需要自己完成一些工作,以便学习和调试代码。

“给一个人一条鱼,你喂他一天;教一个人钓鱼,你喂他一辈子。”

首先,您使用mysql_real_escape_string()作为MySQL_函数,在当前代码中不与MySQLi_混合。

因此,您需要使用其MySQLi_等效项mysqli_real_escape_string(),并且它要求第一个参数是数据库连接。

即:

$username= mysqli_real_escape_string($mysqli, $_POST['email']);

参考功能手册:

并对其他的做同样的事情,并在连接到数据库之后放置它们,或者将数据库连接放在代码的顶部而不显示当前的条件语句。

然后您正在执行作业=而不是比较==

if($user==$username && $pass=$password)
                            ^ there

它应该以{{1​​}}显示为两个等号。

现在在第一个文件中,您应该将$pass==$password放在login.php文件的顶部。如果您的数据库连接失败,它将引发错误,并将被视为在标题之前输出。

更多相关信息:How to fix "Headers already sent" error in PHP

您也没有为此发布HTML表单,如果您对POST数组的输入使用相同的名称属性,并且如果表单使用POST方法,则不知道。

即:

session_start();

然后,对于view_profile.php文件中会话数组的条件语句,给出了误报。

<form method="post" action="your_handler_file.php">
Email: 
<input type="text" name="email">
etc.
</form>

这被解释为:

  • 如果会话mysesi数组未设置且未设置等于付费

而非预期:

if (!isset($_SESSION['mysesi']) && !isset($_SESSION['mytype'])=='Paid')

将被解释为:

  • 如果未设置会话mysesi数组且会话数组等于付费

同样适用于index.php文件中的那个:

if (!isset($_SESSION['mysesi']) && $_SESSION['mytype']=='Paid')

以相同的方式解释,应该读作:

if (!isset($_SESSION['mysesi']) && !isset($_SESSION['mytype'])=='Unpaid')

NOTA:您需要记住,付费付费这两个字区分大小写。因此,如果数据库中的这些单词付费无偿是小写字母,那么这将失败。

重要的是那些匹配。

同样的事情适用于if (!isset($_SESSION['mysesi']) && $_SESSION['mytype']=='Unpaid') if($type=="Paid")

error reporting添加到文件的顶部,这有助于查找错误。

else if($type=="Unpaid")

旁注:只应在暂存时进行显示错误,而不是生产。

并使用<?php error_reporting(E_ALL); ini_set('display_errors', 1); // Then the rest of your code 检查您的查询是否存在错误。但是,该函数需要数据库连接作为参数。

即:mysqli_error()

您可以使用的其他工具包括:

<强>密码

我还注意到您可能以纯文本格式存储密码。不建议这样做。

使用以下其中一项:

其他链接:

关于列长的重要说明:

如果您决定使用print_r();或兼容包(如果PHP <5.5)https://github.com/ircmaxell/password_compat/,请务必注意,如果您当前的密码列的长度低于60 ,它需要更改为(或更高)。手册建议长度为255。

您需要更改列的长度并重新开始使用新哈希才能使其生效。否则,MySQL将无声地失败。

准备好的陈述:

您应该在MySQLi_中使用prepared statement,或使用PDO使用prepared statement

<强>脚注:

password_hash()未知,如果与提交按钮相关。如果是,你不需要转义它,而是使用这样的条件语句:

$_POST['login_user']而不是if(isset($_POST['login_user'])){...}

其余由您决定是否使代码正常工作。

错误检查链接以咨询: