Header() location opens inside div not redirect whole page

Hello, i have page where user can use search bar dynamically. Inside search.php i check if user has rights to search in specific page, and if user has no rights i redirect user with
header("Location: ".$page."");

I have page main.php?view=lines where i show in table user related information inside <div id="search"></div> before it there is input <input type="text" id="searchWait" onkeyup="getSearch(this.value)"> that works as search bar calling getSearch()


<script type="text/javascript">

	function getSearch(value) {
		id = '<?=$id;?>';
		var search = $.post("search.php", {value:value, id:id},function(data){
			$("#search").html(data);		
		});			
		
	}

</script>

If inside search.php action fails i want it to redirect to main.php with

		if(something){
			header("Location: main.php");
			die(0);			
		}

The issue is that page loads inside search div <div id="search"></div> instead of redirecting whole page.

How to solve this? Thanks in advance!

It is difficult to understand with so little information, but if you have a header() within a <div> then it cannot redirect as you have already sent headers for that page.

Perhaps you can supply the full code.

2 Likes

Whats so hard to understand? header() works but it loads link inside that specific div instead of redirecting whole page.

That makes no sense. If it’s in the div, it won’t work. If it’s on top, it would redirect. If you don’t exit, there may come some other content. header() doesn’t load anything, it’s a command for the browser.

I edited post!

Well, it will. When you get the response from the Ajax call, you put the data into that div:

$("#search").html(data);

As you’re calling the PHP code via Ajax, anything you output from that PHP is captured and returned in data, so in this case your header redirect is working, and the output (whatever is in main.php) is returned in data and you put it into the page element called #search.

You’d be better returning an error code to the calling Ajax, and have that do the redirect.

You hadn’t mentioned calling it via Ajax before, hence everyone presuming that you had output the html to create the div. That’s why showing the code is important, or at least giving a full description of how it’s all laid out.

2 Likes

Here is what you need to understand, the point @Gandalf was making:-

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.
https://www.php.net/manual/en/function.header.php

1 Like

You’ve (almost) all missed the rather glaring issue of runtimes.

He’s using Javascript to execute a PHP script and then put the results into a div back in the javascript runtime.

the header() command isn’t being executed in the wrong place in search.php; the fact that it’s being executed at all is the problem.

The solution to the problem isn’t a PHP one, it’s a Javascript one.

If the user does not have the access rights you expect them to have, rather than having PHP execute a header(), have it echo out something like ###NORIGHTSALERT### and die. (something that a search would never return, is the point)

Back in your javascript, rather than just sticking data into your #search div, check to see if data = ###NORIGHTSALERT###.
If so, execute the line window.location.href = "main.php"

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.