-4

Before you mark this duplicate or mark down, please note none of the solutions I found online solved my problem. So I am trying to redirect people to /home.php after everything is uploaded.

<?php
    session_start();
    ini_set('display_errors',1);
    error_reporting(E_ALL | E_WARNING | E_NOTICE);
    include('head.php');
    $title = $_POST['title'];
    $title = stripslashes($title);
    $title = mysqli_real_escape_string($conn, $title);
    $preview = $_POST['preview'];
    $preview = stripslashes($preview);
    $preview = mysqli_real_escape_string($conn, $preview);
    $thumbnail = $_POST['thumbnail'];
    $thumbnail = stripslashes($thumbnail);
    $thumbnail = mysqli_real_escape_string($conn, $thumbnail);
    $category = $_POST['category'];
    $category = stripslashes($category);
    $category = mysqli_real_escape_string($conn, $category);
    $content = $_POST['content'];
    $content = stripslashes($content);
    $content = mysqli_real_escape_string($conn, $content);
    $content = str_replace('\r', '', $content);
    $content = str_replace('\n', '</p><p>', $content);
    $content = str_replace('</p><p> </p><p>', '</p><p>', $content);
    $content = str_replace('</p><p></p><p>', '</p><p>', $content);
    $content = str_replace('</p><p>\r\n</p><p>', '</p><p>', $content);
    $content = str_replace(' i ', ' I ', $content);
    //To capitalize I's.
    $author = $_SESSION['id'];

    if ($_SESSION['rank'] == 2) {
        $status = 1;
    }
    else {
        $status = 0;
    }

    $sql = mysqli_query($conn, "INSERT INTO article (title, preview, author, thumbnail, section, content, status) VALUES ('$title', '$preview', '$author', '$thumbnail','$category', '$content', $status)");
    if ($sql == true) {
        $section = strtolower($category);
        print $section;
        flush();
        header('location:/home.php');
    }
    else {
        print "Did not work.";
    }
?>
<style>
html {background: #FFFFFF;}
</style>

I know it has nothing to do with the html part because the code was not working before I added it in. I have all error reporting turned on so I have no idea what the problem is in my code.

J. DOe
  • 1
  • 2
  • You don't describe what the problem is – John Conde Jul 07 '16 at 00:33
  • The problem is that the code acts as if header(); is not there. – J. DOe Jul 07 '16 at 00:34
  • `header("Location: FULLURL");` Field names are case-insensitive. –  Jul 07 '16 at 00:36
  • I'd really prefer to just be able to use relative URLs if at all possible. – J. DOe Jul 07 '16 at 00:37
  • give tit a test any way, make sure ther is a space after the colon. you can get the full url via code –  Jul 07 '16 at 00:38
  • Tried it out, didn't work. – J. DOe Jul 07 '16 at 00:39
  • then something 'else' is going on here are you sure the code even gets to that point? if the querry fails it could be stopping before the header line –  Jul 07 '16 at 00:40
  • I am sure that the query does not fail, I've tested it with an if statement in my code. – J. DOe Jul 07 '16 at 00:45
  • move the header call to the fist line, see if it works –  Jul 07 '16 at 00:47
  • Wait, what is this? `header('location: profile');` right after you declare $sql? – Michael Hommé Jul 07 '16 at 00:47
  • Took that park out completely. STILL DOESN'T WORK!!! – J. DOe Jul 07 '16 at 00:49
  • Anyone got any more solutions or is this page cursed? – J. DOe Jul 07 '16 at 00:52
  • does a header call in a page all by itself work? .. whats in head.php? –  Jul 07 '16 at 00:53
  • Yeah, it does work. – J. DOe Jul 07 '16 at 00:54
  • 1
    ok so slowly add this code in, untill you find what breaks it –  Jul 07 '16 at 00:54
  • you code above, one line at a time –  Jul 07 '16 at 00:55
  • I literally commented and uncommented every single line and nothing. – J. DOe Jul 07 '16 at 01:04
  • Ok so I used an 'or die' thing after header and it showed it. So there's something wrong with header itself. – J. DOe Jul 07 '16 at 01:09
  • 2 things. 1) Add `or die(mysqli_error($conn))` to `mysqli_query()`. If your query fails, so will your header. 2) Are you running this as `file:///file.xxx` or as `http://localhost/file.xxx` or on a hosted site? You'll need to "ping" me to get my direct attention or any other by starting with the `@` symbol, followed by their name. Do that, then tell/ping me about those two points. Your `else { print "Did not work."; }` is not helping you; get the real error as I stated or as `else { print "Did not work." . mysqli_error($conn); }` - Try getting rid of `flush();` too. – Funk Forty Niner Jul 07 '16 at 01:10
  • I'm doing this on a website server. – J. DOe Jul 07 '16 at 01:15
  • I tried everything on there but nothing. – J. DOe Jul 07 '16 at 01:15
  • THIS IS ABSOLUTELY MADDENING – J. DOe Jul 07 '16 at 01:17
  • @J.DOe Btw, `$thumbnail = $_POST['thumbnail'];` suggests you're trying to handle a file here, and that is usuallly `$_FILES` and not `$_POST`. We have no idea what the HTML file looks like, or if it contains a "post" method. If it doesn't, then `
    ` defaults to a GET method and your code will/could fail silently because of it. My comment above was a 2-part; you answered "one". Plus, you didn't say if your query works or not. If it doesn't then MySQL will also fail silently if your column lengths are too short. There isn't anything I can say here; that's all that comes to mind.
    – Funk Forty Niner Jul 07 '16 at 01:19
  • Actually $thumbnail is just a URL. – J. DOe Jul 07 '16 at 01:20
  • Query works as well. – J. DOe Jul 07 '16 at 01:21
  • well I'm done, i would need direct access to debug this. –  Jul 07 '16 at 01:26
  • What is in head.php? I don't see reference to it in the thread (or maybe I missed it...it's a long thread) – Rasclatt Jul 07 '16 at 01:26
  • 2
    `print $section; flush(); header('location:/home.php');` you're outputting before header here. I now give up on your question; you say no errors, I find that rather hard to believe. Edit: @Dagon me too. – Funk Forty Niner Jul 07 '16 at 01:26
  • @Rasclatt I thought about it earlier, but the guy says the query works. *"Query works as well. – J. DOe 5 mins ago"* - It's probably connection stuff. – Funk Forty Niner Jul 07 '16 at 01:27
  • possible duplicate of [How to fix “Headers already sent” error in PHP](http://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Funk Forty Niner Jul 07 '16 at 01:44

1 Answers1

0

Try adding exit; below the redirect.

header('location:/home.php');
exit;
Michael Hommé
  • 1,696
  • 1
  • 14
  • 18