I am having trouble with my Redirect Function

Discussion in 'PHP' started by AndrewSox, May 8, 2013.

  1. #1
    this is in my config.php
    function redirect($url){
        header('Location: '.$url);
        }
    PHP:
    this code is in my edit-catchment.php
    redirect('index.php');
    PHP:
    but now i receive the following error

    now this is bugging me so much as i cannot find such an error. I have cleared all of the white spacing double checked the spelling.

    I am truly in need of other opinions here, maybe a fresh set of eyes will be able to spot what i am missing.
     
    AndrewSox, May 8, 2013 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Check header.php at line 7. If you need further help, post the contents of that file.
     
    nico_swd, May 8, 2013 IP
  3. dujmovicv

    dujmovicv Member

    Messages:
    62
    Likes Received:
    2
    Best Answers:
    4
    Trophy Points:
    48
    #3
    If you want a quick fix, try to add
    
    ob_start();
    
    PHP:
    at the very beginning of your script. Other than that, you can use a JavaScript call to redirect like :
    
     
    <script type="text/javascript" language="javascript">
       setTimeout("location.href='index.php'", 0);
    </script>
     
    
    Code (markup):
     
    dujmovicv, May 8, 2013 IP
  4. AndrewSox

    AndrewSox Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #4
    @ nico_swd, right so this is the content of my header.php
    <?php
    include '../config/config.php';
    ?>
    <html>
        <head>
            <title>**title**</title>
            <link type="text/css" rel="stylesheet" href="<?php site_root();?>css/styles.css"/>
        </head>
        <body>
            <div id="headerdoc">
                <h1>**header**</h1>
            </div>
            <div class="menu">
                <ul>
                    <li><a href="<?php site_root();?>admin/index.php">Home</a></li>
                </ul>
            </div>
            <div id="under_layer">
                <div id="wrapper">
                    <div id="content">
    PHP:
    i do not see anything wrong here.

    @dujmovicv, I have already tried the ob_start(); and that makes no change.
     
    AndrewSox, May 8, 2013 IP
  5. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #5
    You are not able to change header when anythins is printed in the page. You only can call header function before "print" and "echo".
     
    Hamidsam, May 8, 2013 IP
  6. AndrewSox

    AndrewSox Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #6
    So basically what you are saying is that i cannot have my site_root() function on this page
     
    AndrewSox, May 8, 2013 IP
  7. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #7
    Yes, you do. but it's not possible to call redirect() in site_root().

    Solution:
    1. Alternatives like meta tag or javascript location
    2. Using ob_contents to put output in a variable, then if output was ok, print that.
     
    Hamidsam, May 8, 2013 IP
  8. AndrewSox

    AndrewSox Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #8
    ob_content is an area i have not gone into much, is there perhaps a reference of a website that i can read more about it.

    also would it not be a better quick fix to just get rid of the site_root() function and manually put in the site root
     
    AndrewSox, May 8, 2013 IP
  9. Hamidsam

    Hamidsam Greenhorn

    Messages:
    18
    Likes Received:
    1
    Best Answers:
    1
    Trophy Points:
    23
    #9
    ob_get_content simple example from php.net:
    
    <?php
    
    ob_start();
    echo "Hello ";
    $out1 = ob_get_contents();
    echo "World";
    $out2 = ob_get_contents();
    ob_end_clean();
    
    var_dump($out1, $out2);
    
    ?>
    
    PHP:
    As a quick fix, just call header() and redirect() out of site_root().
     
    Hamidsam, May 8, 2013 IP
  10. AndrewSox

    AndrewSox Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #10
    how would i implement this with my current header.php
     
    AndrewSox, May 8, 2013 IP
  11. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #11
    After the header() in redirect method, add an exit() or die() statement right after, your code should work.

    If that doesn't work. Download notepad++, open the header/config files then Menu -> Encoding -> utf8 without BOM. One of the files should have some weird characters (sequence bytes).

    Another solution I find helpful is copying the file contents, opening notepad and pasting it in a different file. Then overwrite the old file with the new one.
     
    ThePHPMaster, May 8, 2013 IP
  12. Dangy

    Dangy Well-Known Member

    Messages:
    841
    Likes Received:
    25
    Best Answers:
    2
    Trophy Points:
    155
    #12
    <?php
    ob_start();
    function redirect($location){
    header('location:'.$location);
    }
    redirect('index.php');
    include '../config/config.php';
    ob_flush();
    ?>
    PHP:
    Give this a try.
     
    Dangy, May 8, 2013 IP
  13. AndrewSox

    AndrewSox Greenhorn

    Messages:
    6
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #13
    i have given this a try but it is still not working as the function is in a config page and the calling of the function is on the edit page and in between the function and calling of the function there is a include'header.php', and in the header.php i have echo out the site root function and it is conflicting with the redirect function even if i put the ob_start and ob_flush.
     
    AndrewSox, May 9, 2013 IP