Php redirect and refresh redirected - php

I have page1.php from witch I update picture.
Updating is fine and after update, I redirect this page to picture.php:
header('Location: picture.php');
Redirect working fine,but problem is,there is no picture. If I refresh page,manualy, picture show on page.
I also try this:
header("Refresh:0; url=page2.php");
So, is there posibility to redirect from page1.php to picture.php,and after that,redirect picture.php, so user can se image withouth refres page?
Tnx

There are two parts to this:
Refreshing PHP file structure memory, and refreshing the browsers cache.
1)
You need to use clearstatcache(); ( http://php.net/manual/en/function.clearstatcache.php ) in order to tell PHP to refresh the file listing it keeps in its memory.
This should be done at the very top of the picture display page.
Try this if method 2 - below - does not work.
2)
You can also force the browser to refresh the image URL by appending the URL with a random number so you force the page to refresh its search for the image, because
file.jpg?457458458754 is not the same as file.jpg?97767536436 in the <img> tag.
example:
<img src="<?php print $imageFilename.mt_rand(1111111,99999999); ?>">

I think the browser retrieving the page from the local chache, to avoid try this:
header('Location: picture.php?nocache='.time());
In the 99.99999% of case browser use the entire url for cache resources.
But some browsers apply a different cache strategy.

Related

redirect to some page and than come back to the script

Can somehow be managed to redirect to some page using header and after some time to get back to the script and redirect to some another page over and over?!
i use it as localhost on my pc i read urls from text file and i need to redirect to them one by one, that's why i need to come back to the script automatically after redirection
You could create a loop to link to both pages by simply doing this on both pages:
sleep(5); //for 5 seconds
header("location: anotherphpfile.php");
You can use iframe.
<iframe src="http://www.w3schools.com"></iframe>
http://www.w3schools.com/tags/tag_iframe.asp
For testing just do this:
1-Create a page and rename it p1.php
<?php
//This is p1.php
header("location:p1.php");
?>
2-Create another page and rename it p2.php
<?php
//This is p2.php
header("location:p1.php");
?>
3-Now run the p1.php
This is the Result in diffrent browsers:
and if you are going to get a URL and then fetch some information from that page use this function:
file_get_contents and then fetch information from that page, This is what Google and other search engines do.

Action one link and redirect to another using php or javascript

I’ve been battling with this for hours, I wonder if anyone can help.
I want to make a redirect script which first actions a link. I have a link generated by php which deletes the current user’s avatar. This link works (user avatar is deleted) however the link itself doesn’t lead anywhere, it just reloads whichever page it is launched from (I haven’t quite worked out how yet, I presume this is a feature of wordpress/buddypress which I am using). My aim is that on arrival to a particular page (page1.php), the delete avatar link is automatically actioned, and then the user is redirected to another page. So:
1) User arrives at page1.php
2) Script fires this link :
<a href="<?php if ( bp_get_user_has_avatar() ) : print 'mysite.net/members/'; echo userpro_profile_data('user_login', $user_id2); print '/'; bp_avatar_delete_link(); else : 'something-else.php'; endif; ?>"></a
3) User redirected to page2.php
I guess there may be some way to do this in javascript/ajax but I hardly use it so not really sure how. I’m struggling to get it to work in php also. Any help would be really appreciated.
Thanks.
You can redirect the page via Javascript using Location API:
<script type="text/javascript">
window.location = <?= $new_location ?>;
</script>
Or you can do it in PHP after performing required operations using code like this:
header("Location: {$new_location}");
But notice that if you redirecting via headers you should not echo enything to the page before it.
Or you can use wp_redirect() if youre doing it in Wordpress.

url change without anchor tag and refresh using ajax

As per my client need , redirect the page without anchor tag and refresh page ,
and change the URL as per page appearance. I don't know any idea about this.
I can use ajax for page content. but I don't know the URL change option without
page refresh or redirect. how can I do this with ajax j-query. Any one guide me for this issue.thanks advance
sample url
www.samplesite.com/contact.php -> without anchor tag. and page refresh this url need to work on php.
I think you are looking for info about the new HTML5 History API ( pushstate ), this link has a detailed tutorial.
http://diveintohtml5.info/history.html
You can do this using below function.
if(pageurl!=window.location){
window.history.pushState({path:pageurl},'',pageurl);
}
You can use the following javascript functions
window.location.assign('http://www.samplesite.com/contact.php'); // keeps the current page in browser history
window.location.replace('http://www.samplesite.com/contact.php'); // replaces the current page in browser history
window.location = 'http://www.samplesite.com/contact.php'; // the same as assign() and is backward compatible even with the oldest browsers
jsfiddle

How to prevent old images from appearing on page reloads when there are updated images available

I constantly have to update main images on my site, the user will go to the site but the images won't be the updated versions unless they manually hit refresh. Even by me putting "please hit refresh to view updated images" the users ignore this and I have to e-mail them to hit the refresh button. I've tried having the initial index.html reload to the actual site using Javascript like this
The initial index.html:
document.location.href='index2.php?code=reload_page'
Then on the index2.php:
$the_code = $_GET['code'];
if($the_code == "reload_page")
{
$page = $_SERVER['PHP_SELF'];
$sec = "1";
header("Refresh: $sec; url=$page");
}
else
{
//load page regular
}
I tried it like this, but it didn't work, still has old images until you hit the refresh button. Any other ways of accomplishing this using PHP or javascript/jquery?
The problem with images not refreshing might be an issue with caching in web browser or on the server proxy etc. It is configuration issue and might be not dependant on you. Easy trick to bypass this is to add timestamp to img url. Every time you regenerate your content in index.php just add some query string to your image as this:
<?php
echo '<img src="my_image.png?ts='.time().'" />';
?>
it will trick your browser and proxies on the way that it is another image and prevent caching.
You can use timer to reload your images and get them via AJAX reqest form the other page:
setInterval(function(){
$.ajax({
url: 'index2.php'
}).done(function ( data ) {
$('#image-div').html(data);
});
}, 10000); // wait 10 seconds
Take a look at jQuery.ajax
Put the following in the head section of your page this will reload your content every 5 seconds.
<meta http-equiv="refresh" content="5">
If the browser still caches the images because the url hasn't changed then place a random query string on the end of the image url. You could use a timestamp.
<img src="image.jpg?<?php echo time(); ?>">

Cache problem after sending a html-form

in some part of my webpage i allow my users to change their profile picture. This is made simple by a form with the element <input type="file" name="avatar" id="avatar" />. After they upload the photo a php script is called were all the image checking and processing is made. When everything is ok, the previous user image is deleted and changed by the new one (with the same name) and then the user is redirected to their profile page.
The problem is, when the user change his picture, the firts time he goes to his profile page (when is redirected by the upload script) the picture is not the new one, is a cached copy of the old one, after a few f5 (reloads) the new image is showed.
A while ago a have a similar problem with an rss parser i made in php, if i call the url feed sometimes instead of a new version of the feed, i got a cached version. I solved this problem just by generating a ramdom number every time i needed the feed and then adding it to the url like; www.page.com/thefeed.rss?var=#ramdom_number
But, i really dont want to implement this "solution" because is unprofessional and my users will see the url with that parameter.
This is a resume of the upload operation:
profile.php?i=mycv : In this page is all the user data included the actual profile picture and the form to upload a picture, the form makes a post call to image_handler.php
image_handler.php : Is a php script who process the image sended by profile.php?i=mycv and is everything is ok, the user is redirected to profile.php?i=mycv.
Thanks for any help!
Try this code in profile.php
<?php
$act = $_GET["a"];
if($a == "return"){
header("location:profile.php?i=mycv");
}
?>
And then change url in image_handler.php that it sends user to profile.php?a=return page instead of profile.php?i=mycv
If that does not work you can add like this: <img src="http://someplace.com/someimage.png?<?php print time(); ?>"> then user cannot see the number part.

Categories