-3

I am working on a database and I need to redirect the user to another page after the login process.

I am using the redirect_to() function. After the code executes it gives me the error

Fatal error: Call to undefined function redirect_to()

Here is what I tried

if($result){
        $num_rows = mysqli_num_rows($result);

        if($num_rows == 1){

        $found_user=mysqli_fetch_array($result);
        redirect_to("main.php");
        }

        else{
        redirect_to("index.php");
        }
}
TRiG
  • 10,148
  • 7
  • 57
  • 107

6 Answers6

2

You have to define the redirect_to function before calling it. Try this code

    <?php
        if($result){
            $num_rows = mysqli_num_rows($result);
            if($num_rows == 1){
                $found_user=mysqli_fetch_array($result);
                redirect_to("main.php");
            }
            else{
                redirect_to("index.php");
            }
        }
        function redirect_to($location){
            header('Location:'.$location);
        }
?>
Mad Angle
  • 2,347
  • 1
  • 15
  • 33
1

Try to use header :

<?php
/* Redirection vers une page différente du même dossier */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>
0

redirect_to() isn't a function. You want to use header("Location: main.php");

As this is adding header information, it needs to be loaded before anything is passed to the page (I believe), so don't attempt to print anything out to the page before header() is called.

If you want to do something like that, I suggest using javascript.

danhardman
  • 633
  • 1
  • 6
  • 16
0

redirect_to doesn't exist, or if it does, it's not been included/required correctly.

Add this;

if( function_exists('redirect_to') == FALSE ) { //Future proofing...
   function redirect_to($where) {
      header('Location: '. $where);
      exit;
   }
}

If you already have output, I would advise you looking at using output buffering.

ʰᵈˑ
  • 11,279
  • 3
  • 26
  • 49
0

You can easily redirect using following header("Location: ....) syntax:

header("Location: main.php");
exit;
munsifali
  • 1,732
  • 2
  • 24
  • 43
0

You should equip your project with this function first as others have pointed. Alternatively refactor your code to use the builtin php function -> http://php.net/manual/en/function.http-redirect.php

bulforce
  • 991
  • 1
  • 6
  • 11