php redirect_to() function undefined - php

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");
}
}

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

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

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.

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.

You can easily redirect using following header("Location: ....) syntax:
header("Location: main.php");
exit;

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

Related

ERR_TOO_MANY_REDIRECTS in PHP

My logout.php file is like this. Is there any mistake in my code
logout.php
<?php
session_start();
session_destroy();
header('Location:index.php');
exit;
?>
Here is my index.php file. If I am set $_SESSION['s_activId'] then it is working properly but when I am trying to put condition if $_SESSION['s_activId'] is not set at that time I want to pass header on index page sometimes it works sometimes it does not work.
<?php
include('include/config.inc.php');
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
header("Location:index.php");
}
else
{
$wrong = '';
if(isset($_POST['submit']))
{
$checkLogin = "SELECT userName,password,userType
FROM user
WHERE BINARY userName = '".$_POST['userName']."'
AND BINARY password = '".$_REQUEST['password']."'";
$checkLoginresult = mysql_query($checkLogin);
if($userLoginRow = mysql_fetch_array($checkLoginresult))
{
$_SESSION['s_activId'] = $userLoginRow['userName'];
$_SESSION['s_password'] = $userLoginRow['password'];
$_SESSION['hg_userType'] = $userLoginRow['userType'];
if(!$_SESSION['s_urlRedirectDir'])
{
header("Location:index.php");
}
else
{
header("Location:reminder.php");
}
}
else
{
$wrong = "UserId And Password Is Not Valid";
}
}
}
include("bottom.php");
$smarty->assign('wrong',$wrong);
$smarty->display("index.tpl");
?>
The problem arise in the condition below in index.php:
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
header("Location:index.php");
}
When you logout, you are calling session_destroy() on logout.php and redirecting on index.php and the condition above gets true as s_activId is not set in session and again you are redirecting on index.php (without setting s_activId in session). The above condition will be true until the variable s_activId set in session and because of this you are getting ERR_TOO_MANY_REDIRECTS error.
The solution is, on index.php set the variable s_activId in session before calling the header method. Refer the code below:
if(!isset($_SESSION['s_activId']))
{
$_SESSION['s_urlRedirectDir'] = $_SERVER['REQUEST_URI'];
$_SESSION['s_activId'] = true;
header("Location:index.php");
}
Dont redirect index.php to index.php. you having redirects loop. Also
if you have code below that also can fire add die in if because after
redirect code below still executes. I didnt read your code, maybe
there isnt problems with this but after
header("Location: lalala"); always add die(); or exit();

How do I Redirect to Login page using PHP

I am trying to redirect my php login page so that if user is authorised, it goes to a page (r_index.php) and if the user isn't authorised they go back to the login page (login.html).
This is my code:
<?php
if ("password"=="$password") { // Start the condition ?>
Manage classes
<?php } // End the condition ?>
<?php if ("password"=="") { ?>
Login
<?php }
?>.
What am I doing wrong? How should I resolve it?
replace your code with this:
<?php
if ("password"== $password) {
header("location:r_index.php");
}
else if ($password=="") {
header("location:login.html");
}
?>
If you want to redirect you should use:
header('Location: http://www.example.com/r_index.php');
in your code.
<?php
$accessGranted = false;
if($password == 'password') {
$accessGranted = true;
}
if($accessGranted) {
header('Location: r_index.php');
}
else {
header('Location: login.html');
}
exit;
Actually your syntax is wrong, else there is no problem of using HTML inside php. It will work well and good.
Just make sure not to put your variable inside quotes, and change the statement as follows:
if($password=="password")
and
if($Password==" ")

How to redirect with php

I have a register form for member. But when member registered in my website i want automatically open wellcome.php page. I know it is header("Location: wellcome.php"); but i did it and nothing happen. What can i do? Registered is successfully But does not redirect.
<?php
include("includes/connect.php");
session_start();
if(isset($_POST['submit_bregister'])){
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$re_email = $_POST['re_email'];
$password = sha1($_POST['password']);
$vergi_num = $_POST['vergi_num'];
$sirket_kategorisi = $_POST['sirket_kategorisi'];
$is_kategorin = $_POST['is_kategorin'];
$ulke = $_POST['ulke'];
$sehir = $_POST['sehir'];
if($name==''){
echo"<div class='error_name'>Adınız alanını boş bıraktınız!</div>";
exit();
}
if($surname==''){
echo"<div class='error_name'>Soyadınız alanını boş bıraktınız!</div>";
exit();
}
if($vergi_num==''){
echo"<div class='error_name'>Vergi numaranızı girmediniz!</div>";
exit();
}
if(strlen($vergi_num)>11 || strlen($vergi_num)<0){
echo"<div class='error_name'>Vergi numaranız en az çok 11 hane olabilir!</div>";
exit();
}
if($sirket_kategorisi==''){
echo"<div class='error_name'>Şirket Kategorisi alanını boş bırakamazsınız!</div>";
exit();
}
if($is_kategorisi==''){
echo"<div class='error_name'>İş kategoriniz alanını boş bırakamazsınız!</div>";
exit();
}
if($ulke==''){
echo"<div class='error_name'>Yaşadığınız Ülkeyi boş bırakamazsınız!</div>";
exit();
}
if($sehir==''){
echo"<div class='error_name'>Yaşadığınız Şehir alanını boş bırakamazsınız!</div>";
exit();
}
if($email==''){
echo"<div class='error_name'>E-Mail alanını boş bıraktınız!</div>";
exit();
}
if($_POST['email'] !== $_POST['re_email']){
echo"<div class='error_name'>E-Mail Adresleriniz Eşleşmiyor!</div>";
exit();
}
$check_email = "SELECT * FROM users WHERE email='$email'";
$run = mysql_query($check_email);
if(mysql_num_rows($run)>0){
echo "<div class='error_name'>Bu E-Mail adresi kullanımda!</div>";
exit();
}
$sirket_kategorisi = (int)$sirket_kategorisi;
$query = "SELECT is_kategorisi FROM business_category WHERE id='$sirket_kategorisi'";
$res = mysql_query($query);
$row = mysql_fetch_assoc($res);
$sirket_kategorisi = $row['is_kategorisi'];
$querys ="INSERT INTO `users` (`name`,`surname`,`email`, `re_email`,`password`,`vergi_num`,`sirket_kategorisi`,`is_kategorin`,`ulke`,`sehir`)
VALUES ('$name','$surname','$email', '$re_email','$password','$vergi_num','$sirket_kategorisi','$is_kategorin','$ulke','$sehir')";
$result = mysql_query($querys) or die(mysql_error());
if($result){
header('Location: email');
exit;
}
else {
header('Location: error');
exit;
}
}
?>
The headers cannot be sent after there is any output on the page. The echo is an output, so you cannot send any new headers – in you case the Location header.
You should have the redirect before the echo.
Are you sure this header("Location: wellcome.php"); is at the end of your code?
What i mean with that is for example if you have an if what waits for the Get argument, something like:
if(isset($_GET['register']){
//... code for registration
}
please make sure that the header("Location: wellcome.php"); is at the end of if but inside of the bracket, and its good to use exit(); after the header.
Also you cant echo something before the header();, you should have an error when you do that, check if you are getting one.
Some of your code will help a lot to figure whats wrong.
Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. It is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before header() is called. The same problem exists when using a single PHP/HTML file.
<html>
<?php
/* This will give an error. Note the output
* above, which is before the header() call */
header('Location: http://www.example.com/');
exit;
?>
Normally something like this will result in a Cannot modify header information - headers already warning unless you don't have error/warning reporting turned on.
PHP doesn't allow output before header becuase it will redirect to the page straight away using header(), so there is no point outputting a message before you use it. If you try it will cause an error.
You could, however, add in the header of that page
<meta http-equiv="refresh" content="5;url='http://www.your-website.com/welcome.php'" />
The content attr is broken down into 2 sections time(sec) and url i.e.
content="(time in seconds) ; url='(url)'"
You can change either of these, however, make sure you keep them both inside content=""

PHP Cannot redirect even if conditions are met

Here is my code
<?php
if (!isset($_SESSION)) { session_start(); }
if (!isset($_SESSION['username'])) { header("Location: index.php"); }
ob_start();
if($_POST) {
$id = $_POST['book_id'];
$command = $_POST['command'];
$sourcePage = $_POST['source'];
} else if ($_GET){
$command = $_GET['command'];
$sourcePage = $_GET['source'];
$id = $_GET['book_id'];
} else {
header("Location: index.php");
}
// if command is 2 then show cart content
if($command == 2) {
showCart();
// if command is 1 then add book to cart
} else if($command == 1) {
addToCart($id);
header("Location: $sourcePage");
// if command is 0, then remove book from cart
} else if($command == 0) {
deleteFromCart($id);
header("Location: $sourcePage");
} else if(!isset($command)){
header("Location: index.php");
}
ob_flush();
?>
Why is it that even if I'm not logged in, I'm not redirected?
is it possible that the page is simply refreshing under the condition that $_POST or $_GET exists, falling into one of the later header("Location: ...") commands?
If so, you'd want to fix the problem by adding a die();
if (!isset($_SESSION['username'])) { header("Location: index.php"); die(); }
Using exit() or die functions may fix the problem. But there is only very very limited amount of situations where actually need to use one of these functions.
I think you can enhance if else conditions by putting some more conditions. But this will increase your lines of code.
From my experience, every time there is redirect via headers, its following connected code tends to execute.
For example : if you have an else/else if along with an if(which has the redirect code) then they will also be executed and the redirect never happens. However if you break up the conditions into individual ifs then after entering one if if a redirect is present such that there is no succeeding code after that header code in the if then the redirect will happen.
Better to use die()/exit() all over to avoid discrepancies.

About header error in PHP

I am working on a PHP project where I am trying to redirect from one page to another, using header (Location:something.php). It's working fine on the local machine but when uploaded to the server, the following error occurs:
Warning: Cannot modify header information - headers already sent by (output started at /homepages/4/d404449574/htdocs/yellowandred_in/newTest/editHome.php:15) in /homepages/4/d404449574/htdocs/yellowandred_in/newTest/editHome.php on line 43
I already tried with include, include_once, require and require_once but it's giving other errors like:
Cannot redeclare logged_in() (previously declared in C:\wamp\www\ynrNewVersion-1\editHome.php:3) in C:\wamp\www\ynrNewVersion-1\adminIndex.php on line 5
My code
<?php
session_start();
function logged_in() {
return isset($_SESSION['username']);
}
function confirm_logged_in() {
if (!logged_in()) {
include "error404.php";
exit;
}
}
confirm_logged_in();
require_once("connection.php");
$query="select * from home";
$homeInfo = mysql_query($query, $connection);
$result = mysql_fetch_array($homeInfo);
$content = $result['content'];
$rssFeeds = $result['rssFeeds'];
$message = "";
if(isset($_POST['submit'])){
$content = $_POST['content'];
$rssFeeds = $_POST['rssFeeds'];
if($content == "" || $rssFeeds == ""){
$message = "Please enter into all the fields";
} else {
$query = "UPDATE home SET content='$content',rssFeeds='$rssFeeds' WHERE id=1 LIMIT 1";
$result = mysql_query($query,$connection);
if(!$result){
echo "error".mysql_error();
}
if ($result == 1) {
header("Location:adminIndex.php");
} else {
$message = "Error Occurred";
}
}
}
if(isset($_POST['cancel'])){
header("Location:adminIndex.php");
}
?>
Use ob_start() at the top of PHP page;
you need to put an exit() or die() after the header function - otherwise the rest of the script will continue to execute.
The redirection can take a relative or absolute URL. The problem is with the space BEFORE the colon. Try it like this:
header("Location: adminIndex.php");
die();
The problem is that you have echo before your header function.
if(!$result){
echo "error".mysql_error();
}
before your two header("Location:adminIndex.php"), your can't echo anything.

Categories