繁体   English   中英

如何使用脚本window.location重定向页面?

[英]How to redirect page using script window.location?

码:

<?php
   session_start();
   include('conns.php');
   $collegename = $_SESSION['college_name']; 
   if(isset($_POST['lo_gin']))
    {
      $email2 = $_POST['email2'];
      $pass = $_POST['password2'];
      if(empty($email2) || empty($pass))
        {
          $msg2 = "<p id='about_error'>Enter Both the Fields.</p>";
        }
      else
        {
          $sql="SELECT * from students where email = '$email2' and password = '$pass'";
          $r = mysqli_query($link,$sql);
          $num_rows = mysqli_num_rows($r);
          if($num_rows == 0)
           {
             $msg2 = "<p id='about_error' style='color:red;text-align:center;'>Wrong Email or Password</p>";
           }
          else
           {
             $row=mysqli_fetch_array($r,MYSQLI_ASSOC);
             $_SESSION['student_id'] = $row['student_id'];
             if(isset($_SESSION['student_id']))
               {
 ?>
    <script>
      window.location='http://collegescan.in/student-dashboard/public/index.php';
    </script>
    <?php 
      }
       elseif ($collegename !='')
      {
    ?>
    <script>
        window.location='view.php?college_name='.<?php echo $collegename; ?>.'';
    </script>
    <?php
      }
    }
  }
}
?>

在这段代码中,我将使用$_SESSION['college_name']并将其初始化为$collegename ,当我单击登录按钮时,它将重定向到index.php页面,但我想首先重定向view.php。 那么,如何解决这个问题呢?

试试这个,你会得到答案的

$(location).attr('href','domain.com/view.php?college_name');

上面的代码是jQuery

只需使用php标头功能重定向,您的代码就会更好:

     <?php
       session_start();
       include('conns.php');
       $collegename = $_SESSION['college_name']; 
       if(isset($_POST['lo_gin']))
        {
          $email2 = $_POST['email2'];
          $pass = $_POST['password2'];
          if(empty($email2) || empty($pass))
            {
              $msg2 = "<p id='about_error'>Enter Both the Fields.</p>";
            }
          else
            {
              $sql="SELECT * from students where email = '$email2' and password = '$pass'";
              $r = mysqli_query($link,$sql);
              $num_rows = mysqli_num_rows($r);
              if($num_rows == 0)
               {
                 $msg2 = "<p id='about_error' style='color:red;text-align:center;'>Wrong Email or Password</p>";
               }
              else
               {
                 $row=mysqli_fetch_array($r,MYSQLI_ASSOC);
                 $_SESSION['student_id'] = $row['student_id'];
                 if(isset($_SESSION['student_id']))
                   {

         header('Location: http://collegescan.in/student-dashboard/public/index.php');
exit;


          }
           elseif ($collegename !='')
          {
header('Location: view.php?college_name='.$collegename);
        exit;

          }
        }
      }
    }
    ?>

最后一条else语句完成后

      else
       {
         $row=mysqli_fetch_array($r,MYSQLI_ASSOC);
         $_SESSION['student_id'] = $row['student_id'];
         if(isset($_SESSION['student_id']))
           {
           }
       }
header('Location: view.php')

或位于哪个文件夹中的view.php

暂无
暂无

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

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