-2

I've made this block of code:

$tag = $_GET['tag'];

if ($_SERVER[HTTP_REFERER'] == 'http://sitename.com/portfolio' || $_SERVER['HTTP_REFERER'] == 'sitename.com/tags/?tagname=' . $tag ) : ?>

<figure class="folio-item">
    <img src="<?php echo $mod->src; ?>" alt="">
</figure>

<?php  else : ?>
   <?php header('http://sitename.com/portfolio'); ?>
<?php endif ?>

So if the statement is true it has to make the figure, which does work. but when the statement isn't true it gives me a empty file instead of heading me to sitename.com/portfolio

how can i make this work?

the Solution:

$tag = $_GET['tag'];

if ($_SERVER[HTTP_REFERER'] == 'http://sitename.com/portfolio' ||       $_SERVER['HTTP_REFERER'] == 'sitename.com/tags/?tagname=' . $tag ) : ?>

<figure class="folio-item">
    <img src="<?php echo $mod->src; ?>" alt="">
</figure>

<?php  else : ?>
   <?php header('Location:http://sitename.com/portfolio'); ?>
<?php endif ?>

I added Location: to the header function like Brian said.

Floeske
  • 100
  • 1
  • 12
  • 1
    syntatically speaking `$tag = $_GET['tag']` is missing a semi-colon and `$_SERVER[HTTP_REFERER']` is missing a quote and isn't reliable to use neither. – Funk Forty Niner Jun 30 '15 at 19:15
  • ohhh, I did found the problem, the semi-colon exists in my file, didn't type it here. must be – Floeske Jun 30 '15 at 19:16
  • 1
    then look up on how to use header http://php.net/manual/en/function.header.php – Funk Forty Niner Jun 30 '15 at 19:17
  • Is the code you just added your code or the code in the question? Please update. – chris85 Jun 30 '15 at 19:18
  • do not exactly know what you mean, the code I added in the comments is the solution to my problem – Floeske Jun 30 '15 at 19:21
  • In regards to `$_SERVER['HTTP_REFERER']` you should read this http://stackoverflow.com/a/6023980/ as to why it isn't reliable. There are other/better ways to do that. Oh and this `$_SERVER[HTTP_REFERER']` will fail as I said. the missing quote. So, for what you posted, I can't see how that will ever work. Update your question for the right syntax. http://php.net/manual/en/function.error-reporting.php will say differently. – Funk Forty Niner Jun 30 '15 at 19:22

1 Answers1

3

you have to tell it that you are setting a location rather than another header attribute

<?php  else : ?>
   <?php header('Location: http://sitename.com/portfolio'); exit; ?>
<?php endif ?>

the exit; statement tells it to stop executing which allows the redirect. You also need to make sure you don't spit out any html before you redirect.

Cohan
  • 4,384
  • 2
  • 22
  • 40
  • Yeah thanks, that's it! Im going to use exit for sure, didn't thin about spitting html before the redirect cause that isn't going to work – Floeske Jun 30 '15 at 19:17
  • 1
    It'll work without the exit, but if a browser is misbehaving, more PHP output could be observed, so it's always a good idea to put an exit after redirection. – Dave Chen Jun 30 '15 at 19:20
  • glad i could help. And Dave is right, you don't really need it, but I've had issues come from it and the `exit;` helped a lot. – Cohan Jun 30 '15 at 19:30