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.
Related
I've create a project in PHP for my class and it is to replicate the google signup pages and when the information are correctly entered it writes the user's info in a file called "users.txt". Here is the github link to the source files : https://github.com/Ryan30012/myProject
The first page is the index page, everything works great, but to submit the information you have to click the next button two times. Then comes the page with the error which is the "process.php". If you leave everything empty and you click on submit button it should output an error about some empty field, but instead nothing happens because there is a warning that appeared in the browser console which is this:
Thank you for your help!
I figured what was the problem. It what in condition of the form:
The variable $error was not define so I've just added the line:
$error = true;
After declaring all the other variables. Now everything works.
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.
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.
I've a front-end form with a file input where anybody (no registered users) can upload an image that will be attached to a custom meta field in the back-end. To preview the image I'm using the old iframe technique. My form looks like this:
<form id="upload-photo" method="post" target="preview-iframe" action="<?= get_template_directory_uri() ?>/inc/upload.php" enctype="multipart/form-data" >
<div id="preview"><img src="" alt="" /></div>
<iframe id="preview-iframe" name="preview-iframe" src=""></iframe>
<input type="file" name="author_photo" />
<input type="hidden" id="attachment" name="attachment" value=""/>
<button type="submit" id="upload">Upload</button>
</form>
Then I use WordPress built-in functions to handle the upload and move the file into the media gallery. I use the hidden field to store the WordPress id of the attachment so if users decide to change the picture by uploading a new one then the old one would get removed. This is my PHP:
<?php
define('WP_USE_THEMES', false);
require_once '../../../../wp-load.php';
require_once(ABSPATH .'wp-admin/includes/image.php');
require_once(ABSPATH .'wp-admin/includes/file.php');
require_once(ABSPATH .'wp-admin/includes/media.php');
if (isset($_POST['attachment'])) {
wp_delete_attachment($_POST['attachment'], true);
}
foreach ($_FILES as $file => $data) {
if ($data['error'] === UPLOAD_ERR_OK) {
$attachment = media_handle_upload($file, null);
}
}
echo wp_get_attachment_image($attachment, 'author', 0, array('id' => $attachment));
?>
And finally the jQuery that glues it all together:
var $preview = $('#preview'),
$iframe = $('#preview-iframe'),
$attachment = $('#attachment');
$('#upload').click(function() {
$iframe.load(function() {
var img = $iframe.contents().find('img')[0];
$preview.find('img').attr('src', img.src);
$attachment.val(img.id);
});
});
Everything works perfect but there are few issues with this simple approach:
If JavaScript is disabled images don't get removed
If the user uploads a file then refreshes the site and then uploads and other image, then the previous one wouldn't get deleted because the previous attachment ID doesn't exist due to the refresh.
A malicious user could edit the hidden attachment field with a different ID.
I though about uploading the files to a /temp folder for previewing purposes only and then run a cron job every X time to empty it out. But how do I then make use of WordPress functions to move the image from /temp to the gallery once the whole form has been submitted so I can get and attachment id to link to the post?
Notice that I've two forms, one for handling the image, and the global form with all the content that will be posted and that already works since I can post the new post as "draft" and admins have the power to decide. But how to do this for images securely? How to preview an image and put it in the gallery only if the form has been posted successfully?
I know about the FileReader API but I need compatibility for IE8+ so that won't do. I'm also aware of all the Flash and Silverlight solutions but that's not an option either. Also please don't just link to WordPress plugins, I'm trying to learn here.
Ok, it seems I'm answering my own questions again. This is how I solved it. I found a WordPress function media_handle_sideload that lets you upload files from other locations and not only files from the $_FILES array like the previous function.
So I went with my initial approach now that I know about that function. I basically upload the file to a /temp folder for preview purposes and give it a unique id that I store into the hidden field. When the user submits the overall form and passes validation I take the ID that was stored and find out if the file exists and if so I move it to the gallery. This solves most of my concerns about security because even if a malicious user finds an existing unique ID (unlikely but possible) the file wouldn't get removed like before, but just moved into the gallery (not a big deal).
Finally I set-up a cron job to empty out the temp folder every X amount of time.
Newby here.
Could someone show me an example of the code needed to do the following:
User pushes a button on my web site (there is no information for him to input, and no form, he just clicks on a button). I have found the following code on another post, but don't know if it is correct (I am also getting a syntax error on it):
<form action="php_file.php"><input type="submit" value="Click"></form>
The author of the above code said "Insert your PHP-Code into the file php_file.php and click the button, your file will be opened. Insert header("Location: html_file.html"); at the end of your php-file to get back to the page."
This click of the button needs to instigate the programming to grab the current URL and previous URL and insert them into the mysql database on my server. I have "PHP_SELF" and "HTTP_REFERER", but still need to get the results into mysql.
I would like to do this using only html, PHP and mysql, if possible.
Thanks to everyone for any help!
if your first file happen to be a PHP one, write this HTML form there.
<form action="php_file.php" method="POST">
<input type="hidden" name="previous" value="<?=urlencode($_SERVER['REQUEST_URI'])?>">
<input type="submit" value="Click">
</form>
and then in the php_file.php
<?
$current = $_SERVER['REQUEST_URI'];
$previous = $_POST['previous'];
though both variables will contain only partial url, without host name, schema and, possible, port. it's usually enough but if you need these absent parts, you'll have to add them manually.
as for the writing info into database and particular PHP syntax rules you have to find yourself a tutorial, because this site is devoted to answering questions, not online education nor doing someone's job for free.
With PHP, you can manage it with cookie session, first thing you'll need to do is start a session and then define the space where you'll store the URL information e.g: $_SESSION["url"]
session_start();
$_SESSION["url"]=$_SERVER['REQUEST_URI'];
And whenever you want to go to that particular page, add the header:
header('location: ' .$_SESSION["url"]. '');
Current:
$currentUrl = $_SERVER["PHP_SELF"];
Previous:
$previousUrl = $_SERVER['HTTP_REFERER'];
Note that some users may have browser preferences set that keep $_SERVER['HTTP_REFERER'] from being set, so it's possible that it would come back empty.