hi,
got a new prob, how can i create a php page that automatically refreshes each time i access the page.

hi,
got a new prob, how can i create a php page that automatically refreshes each time i access the page.

I'm sure you dont want to cuase an infinite loop. What are you trying to do?

hi,
got a new prob, how can i create a php page that automatically refreshes each time i access the page.

explain in what way, you expect the PHP to work for you.
Otherwise every page whether PHP or HTML refreshes itself whenever you will refer/call to it

its like this, when i add an item from the drop down menus and submit it, the next form that shows is the table containing the item that i just add but every first item that i add was not displayed. the page only shows the table but the first item that i just added was not there, i can only see only the succeeding items.

its like this, when i add an item from the drop down menus and submit it, the next form that shows is the table containing the item that i just add but every first item that i add was not displayed. the page only shows the table but the first item that i just added was not there, i can only see only the succeeding items.

Just try to start the coding part of it, if you get stucked up somewhere, we will help you, because it still not clear what you trying to achieve.Maybe while looking at your code effort will understand it better.

<?php
header("Location: http://yoursite.com/index.php");
exit;
//above must be at very top before any browser output

ok, let me browse through my code.. i'll also try putting headers. thanks thanks for the reply :-)

Hey cwarn... m new to php but AFAIK header location is used to redirect isnt it? if u paste this code on top of every page, all of them will get redirected to index.php before even getting processed... Correct me if i m wrong. m just curious

<?php
header("Location: http://yoursite.com/index.php");
exit;
//above must be at very top before any browser output

I guess what u wer trying to tell here is header(Cache-Control: no-cache)..

Hey cwarn... m new to php but AFAIK header location is used to redirect isnt it? if u paste this code on top of every page, all of them will get redirected to index.php before even getting processed... Correct me if i m wrong. m just curious

I guess what u wer trying to tell here is header(Cache-Control: no-cache)..

yes, you are right, instead you can put it inside some condition like-

if(some condition)
{
//put the header() here;
//exit();
}

also do we need to use the exit() below it?Will it ever get executed when already redirected to the header-location page?No it will get its turn to execute.
Also ensure that your script never tries to send anything to the client machine before this headers are sent not even a blank space or something.So no echoes before this, otherwise you might get the famous "headers already sent" error

Well actually the entire page will be process by php unless the exit; statement is placed there. So for example if you have a mysql insert it will insert before redirecting unless the exit; is placed immediatly after the header. And yes it will redirect the user to index.php however it is the browser that redirects the user and not the server. Basically when the browser reads that header the browser will then go to that location.

also do we need to use the exit() below it?Will it ever get executed when already redirected to the header-location page?No it will get its turn to execute.

The exit function is used as a safety measure just in case the end user gets to echo something before header function (that will cause an error and header will not redirect).
Here is an example,

<?php
$x = empty($x) ? 1 : $x;
if($x == 1) {
	echo "Welcome";
	header("location: http://www.google.com");
}
echo "This data is supposed to be secure...";
/* more secure stuff here.... */
?>

But if you use exit after the header function, It will exit without processing any further.

<?php
$x = empty($x) ? 1 : $x;
if($x == 1) {
	echo "Welcome";
	header("location: http://www.google.com");
        exit;
}
echo "This data is supposed to be secure...";
/* more secure stuff here.... */
?>

Well actually the entire page will be process by php unless the exit; statement is placed there. So for example if you have a mysql insert it will insert before redirecting unless the exit; is placed immediatly after the header. And yes it will redirect the user to index.php however it is the browser that redirects the user and not the server. Basically when the browser reads that header the browser will then go to that location.

cwarn23 thanks for the elaboration.But are you trying to say that, the mysql insert will be executed in the below code as there is no exit after the header() -

if(something)
{
header("Location:new_url.php");
mysql_query($insert_query);
}

compared to the code below -

if(something)
{
header("Location:new_url.php");
exit;
mysql_query($insert_query);
}

cwarn23 thanks for the elaboration.But are you trying to say that, the mysql insert will be executed in the below code as there is no exit after the header() -

if(something)
{
header("Location:new_url.php");
mysql_query($insert_query);
}

compared to the code below -

if(something)
{
header("Location:new_url.php");
exit;
mysql_query($insert_query);
}

That is correct from what I have read in the past.

yeah dats wat i picked up from his explanation but really... is it so??

cwarn23 thanks for the elaboration.But are you trying to say that, the mysql insert will be executed in the below code as there is no exit after the header() -

if(something)
{
header("Location:new_url.php");
mysql_query($insert_query);
}

compared to the code below -

if(something)
{
header("Location:new_url.php");
exit;
mysql_query($insert_query);
}
<?php
$x = empty($x) ? 1 : $x;
if($x == 1) {
	echo "Welcome";
	header("location: http://www.google.com");
        exit;
}
echo "This data is supposed to be secure...";
/* more secure stuff here.... */
?>

This code will throw an error at line4 saying "headers already sent"
So no echoes before the header() :)

This code will throw an error at line4 saying "headers already sent"
So no echoes before the header() :)

Probably you didn't get my point. I was trying to demonstrate the use of exit after header function. Even though it will throw an error, it will still print This data is supposed to be secure... . Imagine some really secure data instead of echo statement. If you don't have exit, it will execute it (as cwarn23 has already pointed). Using exit will cut the chances of executing unwanted code.

This code will throw an error at line4 saying "headers already sent"
So no echoes before the header() :)

Just to make it clear - indeed there should be no echos before the header().

@cwarn23 : Thanks a lot :)
Here is the everything at one place to make it clean for everybody.
Header() doesn't alter the flow in any way, but tells the user's browser to go to that other page. The script can still output content and the user's browser will receive it, but never show it.
This is the reason why the mysql insert will get executed , because the entire script will get executed too

i could have nevr imagined it...
i thought the control gets transfrrd to d page header is referring to and d script doesnt gets executed at all...
nice info..
cheers :)

@cwarn23 : Thanks a lot :)
Here is the everything at one place to make it clean for everybody.
Header() doesn't alter the flow in any way, but tells the user's browser to go to that other page. The script can still output content and the user's browser will receive it, but never show it.
This is the reason why the mysql insert will get executed , because the entire script will get executed too

thank you guys. Problem solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.