php - php 重定向不起作用

标签 php mysql

crudindex.php 中,如果任何用户使用 admin 和密码 admix 登录,则会回显一些信息并重定向到 crudview.php。 这里的问题是任何点击登录按钮的用户都会重定向到 crudview.php。

要求:对于管理员用户,它将重定向到crudview和其他crudeditusr.php

2) 我需要重新生成 session ID 并添加登录页面的代码吗?

3) key 我可以更改为任意数字吗?

<?php
$con = mysqli_connect("127.0.0.1", "kkits996_ganesh", "", "kkits996_testmysql")  or die("Error " . mysqli_error($con));

// Declare array for errors
$error=array();    
//-----------------------------------------------------//
//---------------------CSRF PROTECT--------------------//
//-----------------------------------------------------//

//generate a token/
function generateToken( $formName )
{   
    //secret_key change it
   $secretKey ='?@GEskki58668445744!Erpoejsj48';
   if ( !session_id() ) 
   {
       session_start();
   }
   $sessionId = session_id();
   return hash('sha512', $formName.$sessionId.$secretKey );
}

//check if the token is valid
function checkToken( $token, $formName)
{
   return $token === generateToken( $formName );
}

//Separate REGISTER AND LOGIN TO NOT BE CONFUSED//

//-----------------------------------------------------//
//---------------------REGISTRATION--------------------//
//-----------------------------------------------------//
if ( isset($_POST['register']) && checkToken( $_POST['csrf_token'], 'userFromRegistration' )  ) 
{
    //if the username required
    if(!preg_match('/^[A-Za-z0-9]+$/',$_POST['uname']))
    {
         $error['username'] = "Username must have alphanumeric characters ";
    }

    //if password has less than 6 characters
    if(strlen($_POST['pwd']) < 6)
    {
         $error['password'] = "Password must be minimum of 6 characters";
    }

    //if password does not match
   if($_POST['pwd'] !== $_POST['cpwd'] OR empty($_POST['cpwd']) ) 
   {
         $error['passwordmatch'] = "Password and Confirm Password doesn't match";
   }

    //if empty error array
    if( !array_filter($error) )
    {
         //trim data
         $username = trim( $_POST['uname'] );

         // Hash you password, never save PASSWORD AS PLAIN TEXT!!!!!!!
         // MYSQL! : Allow your storage to expand past 60 characters (VARCHAR 255 would be good)
         $password = password_hash( $_POST['pwd'], PASSWORD_DEFAULT);

         //if the id is autoincremented leave id
         //----------USE PREPARED STATEMENT FOR SQL INJECTION---//

         $query = 'INSERT INTO cruduser (username, password) VALUES (?,?)';
         $stmt = $con->prepare($query);
         $stmt->bind_param("ss", $username, $password);
         $stmt->execute();
         $stmt->close();
         $con->close();

         //Redirect because we need to consider the post request from crudadd.php
         header( 'Location: crudaddusr.php' ) ;
     }
}

//-----------------------------------------------------//



//------------------------LOGIN as admin---------------------//

if ( isset($_POST['login'])) 
{
    if ($_POST['uname']="admin" && $_POST['pwd']="adminx")
    {
        echo $_POST['uname'];
        echo $_POST['pwd'];    
        $con->close();
        header ("Location: crudview.php");
  }
}

//------------------------LOGIN as Normal-----------------------//

if ( isset($_POST['login']) && checkToken( $_POST['csrf_token'], 'userFromRegistration' )  ) 
{
    //if the username required
    if(!preg_match('/^[A-Za-z0-9]+$/',$_POST['uname']))
    {
         $error['username'] = "Username must have alphanumeric characters ";
    }

    //if password has less than 6 characters
    if(strlen($_POST['pwd']) < 6)
    {
         $error['password'] = "Password must be minimum of 6 characters";
    }

    //if password does not match
   if($_POST['pwd'] !== $_POST['cpwd'] OR empty($_POST['cpwd']) ) 
   {
         $error['passwordmatch'] = "Password and Confirm Password doesn't match";
   }

    //if empty error array
    if( !array_filter($error) )
    {
         //trim data
         $uname = trim( $_POST['uname'] );

         // Hash you password, never save PASSWORD AS PLAIN TEXT!!!!!!!
         // MYSQL! : Allow your storage to expand past 60 characters (VARCHAR 255 would be good)
         //$pwd = password_hash( $_POST['pwd'], PASSWORD_DEFAULT);
         $pwd = $_POST['pwd'];

         $con->close();

         //Redirect because we need to consider the post request from crudadd.php
           header("Location: crudeditusr.php?suname=".$uname."&spwd=".$pwd); 


//         header( "Location: crudeditusr.php?suname=$uname&spwd=$pwd");
     }
}
//-----------------------------------------------------//
//if (isset($_POST['login']))
//{
     //what ever you want
     //Use password_verify() and session_regenerate_id() 
     //to compare passwords and to generate a session id to prevent session fixation.

//} 
//?>

<!--HTMl PART-->
<!DOCTYPE html>
<html>
    <head>
        <title>"Login Registration"</title>
        <!-- bootstrap link is downloaded from bootstrapcdn.com for css and js -->
        <!-- col-mod-6 col-mod-offset are bootstrap related-->
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>
    <div class="container"> 
        <div class="row">
            <form method="post" action="" class="form-horizontal col-mod-6 col-mod-offset-3">
            <input type="hidden" name="csrf_token" value="<?php echo generateToken('userFromRegistration'); ?>" required/>
            <h2>Login Registration</h2>
            <div class="form-group">
                <label for="input" class="col-sm-2 control-label">Username : </label>
                <div class="col-sm-10 <?php if( !empty( $error['username'] ) ){ echo 'has-error';}  ?> ">
                    <input type="text" name="uname" class="form-control" id="input1" placeholder="Username"/>
                    <span class="help-block"><?php if (!empty($error['username'])) echo $error['username'];?></span>
                </div>
            </div>
            <div class="form-group">
                <label for="input" class="col-sm-2 control-label">Password: </label>
                <div class="col-sm-10 <?php if( !empty( $error['password'] ) ){ echo 'has-error';}  ?>">
                    <input type="password" name="pwd"  class="form-control" id="input1" placeholder="Password"/>
                    <span class="help-block"><?php if (!empty($error['password'])) echo $error['password'];?></span>
                </div>
            </div>
            <div class="form-group">
                <label for="input" class="col-sm-2 control-label">Confirm Password : </label>
                <div class="col-sm-10 <?php if( !empty( $error['passwordmatch'] ) ){ echo 'has-error';}  ?>">
                    <input type="password" name="cpwd" class="form-control" id="input1" placeholder="Confirm Password"/>
                    <span class="help-block"><?php if (!empty($error['passwordmatch'])) echo $error['passwordmatch'];?></span>
                </div>
            </div>
            <div class="row">

                <div class="col-mod-6 col-mod-offset-3">
                   <button id="submit1" name="register" class="btn btn-primary pull-right">Register</button>
                   <button id="submit2" name="login" class="btn btn-secondary pull-right">Login</button>
               </div>
           </div>
       </form>
   </body>
</html>

最佳答案

此表单甚至不会发布,因为提交按钮不是 type="Submit"

此外,您需要进行检查以区分单击登录或单击注册时的操作。

您的表格似乎是一种注册表格。在表单标记中添加操作作为 $_SERVER["PHP_SELF"] 。现在,更改

 <button id="submit1" type='submit' name="register" class="btn btn-primary pull-right">
 Register </button>

这会将表单数据发布到同一页面上,并且您的注册检查应该可以正常工作。

关于php - php 重定向不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45707946/

相关文章:

php - 连接两个 mysql_fetch_array

mysql - 如何使用子选择?

php - 如何从mysql记录中获取最后一个值

php - 在 Drupal 管理/内容中获取空白页面

php - 如果提交的表格没有用户名或密码,则重定向到索引页

javascript - 使用 Ajax 捕获 IP 地址

php - 从日期选择器到 mysql/php 的奇怪日期格式

mysql - 根据表中某一列的值从两个不同的表中获取数据

php - 意外转换为 bool 值?

php - 如何显示 mySQL 中的正确数据?