0

I have done a conditional redirect with javascript (depending of referer). However now I need to detect if user got redirected and then he clicked back button and got again to the page he has been redirected from. In this case I need no to redirect him again.

I have found a solution here - but I am failed to combine php and javascript properly, so there is always an error - How do I detect if a user has got to a page using the back button?

Code:

<form name="ignore_me">
<input type="hidden" id="page_is_dirty" name="page_is_dirty" value="0" />
</form>

<script language="javascript" type="text/javascript">
var dirty_bit = document.getElementById('page_is_dirty');
if (dirty_bit.value == '1')  {

document.write("<p>Do Not Redirect</p>");

}
else {

<?php if((stristr($_SERVER['HTTP_REFERER'],"thoughts") != FALSE) { ?>

<?php $setupform = '<form id="form1" method="post" action="http://yahoo.com">'; ?>
<?php $submitform = 'document.getElementById(\'form1\').submit(); </form>'; ?>
<?php echo $setupform;  ?>
<?php echo $submitform; ?>

<?php } ?>
}

function mark_page_dirty() {
dirty_bit.value = '1';
 }
</script>  

What is wrong here?

The error is that it simply doesn't redirect - it gives a blank page with code on it:

<form name="ignore_me">
<input type="hidden" id="page_is_dirty" name="page_is_dirty" value="0" />
</form>

<script language="javascript" type="text/javascript">
var dirty_bit = document.getElementById('page_is_dirty');
if (dirty_bit.value == '1')  {

document.write("<p>My First JavaScript</p>");

}
else {


<form id="form1" method="post" action="http://yahoo.com">document.getElementById('form1').submit(); </form> 
}

function mark_page_dirty() {
dirty_bit.value = '1';
}

Community
  • 1
  • 1
Zox
  • 203
  • 3
  • 8
  • what error? Give us more details. – Robert May 09 '13 at 13:49
  • I added what the error is. It simply doesn't do any redirection. The core redirection works fine i tested but once i added the javascript back button condition it fails. – Zox May 09 '13 at 13:53

1 Answers1

1

I took a look at your code and here are some points:

  • When mixing PHP and JS, remember, that PHP code will be executed first, it will not read any of JS statements

You put a block of PHP code into if statement of JS.

Here is a possible solution for you: PHP logic first:

$redir = false ;
if((stristr($_SERVER['HTTP_REFERER'],"thoughts"))) {
  $redir = true ;
}

HTML form:

<form id="check" action="a.php" method="post">
  <input type="hidden" name="durty_bit" value="1" />
</form>

JS check:

var check = getElementById("check") ;
if (check && check.durty_bit == 1){
  document.write("<p>Do Not Redirect</p>");
} else {
  <?php if ($redir){ ?>
    document.write('<form id="form1" method="post" action="http://yahoo.com"> </form>');
    document.getElementById("form1").submit() ;
 <?php } ?>
}
sybear
  • 7,837
  • 1
  • 22
  • 38
  • Thanks but it doesn't seem to work also the form shouldn't be action one but i hidden one. – Zox May 09 '13 at 14:09
  • After php execution this is the code i am getting - it only seems that these 2 pages doesn't redirect and this is the problem -
    document.getElementById('form1').submit();
    – Zox May 09 '13 at 14:11