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

时间:2017-04-04 13:13:02

标签: javascript 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']))
               {
 ?>
    <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。那么,我该如何解决这个问题呢?

3 个答案:

答案 0 :(得分:0)

试试这个,你会得到答案

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

上面的代码是jquery

答案 1 :(得分:0)

在你的代码中使用php header funciton重定向要好得多:

     <?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;

          }
        }
      }
    }
    ?>

答案 2 :(得分:0)

在你的最后一个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文件夹