I'm building an image slider where images are loaded from my database and displayed inside a php page.
There's a session variable, "number", to remember what is the current database row, in this way I can easily move to next or previous image.
Initially the page was working perfectly, but now when I press on "Next" button to increase session number and move to next page, it remains stuck to "2" and doesn't allow me to read other pages, even if there are multiple ones. I can only browse image no. 1 and image no. 2
I believe the code is correct, but I'm afraid there's something missing about how PHP sessions works, can you confirm it?
//
session_start() must be the second thing on your page after <?php. HTML tags must be placed after it or the session will not be started.
You do not need to use Session to maintain the pagination, instead Keep the pagination status inside Next, Prev html buttons. I could see some better pagination logic here at:
Simple PHP Pagination script
Related
I have a website where the user starts on one page and then moves to a second page. The first page initializes and adds values to a counting variable, which I then want to be able to be displayed on the second page. I know to use a session, but I am wondering how I should properly end the session--do I end it on the first page, or the second page? Do both pages need to begin with session_start()?
Yes. Both must use session start. The variable you are talking about is basically termed cookies. The variables will be shared throughout the browser sessions.
I'm using a session variable to save the last page the user visited in the search results so that they can go back to that page.
if I make
echo($_SESSION['page'])
at he end of the results pages the value is correct, but when I reload the page (or load any other page) the value is increased by 1. I made a echo($_SESSION['page']) right after the session_start() call and it was already increased, so I guess it's been increased right before the PHP code for the search results finishes running but can't find the line of code that is doing so.
In order to test this added a new variable called $_SESSION['page2'] and this new variable is been increased too.
Any ideas?
EDIT
The variable is taking the value from a Pager object like this
$_SESSION['page'] = $pager->getCurrentPageID();
And I can't find any other place where its been set to a different value. We use the same unit and code in a different site and never had this problem before. Tried replacing the code and the Pager class definition with those from the other site and that didn't fix it.
session_start();
Has to be called before anything is echoed to the browser, best practice is to have it as the first thing you have on your PHP page.
Giving you a better answers is no problem, if you provide more code.
More about session_start();
I'm looking at neatening my file structures and code. At the moment I'm making an image gallery, it has a management page which allows the user to upload, edit and delete images/information.
To upload an image, the user would select their files, sumbit the form and arrive at uploaded.php.
To delete an image, the user would click a link taking them to delete.php?id=IMAGEID.
To edit an image, the user would be taken to a page with an editor. Upon making any revisions they'd then be taken to edit.php?id=IMAGEID.
Each page runs either a function. I feel it's unecessary to have three different pages to run three different functions. Is there any way I could neaten this process?
Well, it just depends on how you like to manage your files and how you are using your IDE when you've to edit something.
If you're working alone, it's clearly up to you. If you know someone will, maybe, have to rework your code someday, having three files could be a plus, somehow.
I guess you're not concerned about SEO for these pages, but just in case the problem appears again, it could be better to have actions in parameters, rather than in files name.
/manage.php?edit or /manage.php?delete or /manage.php?upload
But, if your code is really really big, using three files could still result in a lower use of memory (Well, very slight improvement, but still)
Exactly you don't need 3 pages for each function
1)Delete ,instead of redirection the user to a different page and delete it ,you can add an image button which delete icon and when user click on it ,it call a specific function in your php and delete it then refresh your page.
2)Edit and Insert : these are 1 page ,like make your insert page if it doesnt have a query string with the Image ID then it's insert function ,else it contains and ID then load the content of this Image inside your inputs and while saving check if there is ID then update else insert new record
So,you moved from 3 pages to 1 page only.Hope this helped you.
Lets say I have a Page with a List (list.php).
I click on a row on that list to Edit that record. I go to a edit.php Page.
I have 3 buttons on that edit.php page. Save, Apply, Cancel
Save button - Saves the Record and returns to the (list.php) Page
Apply button - Saves Record but stays on the same page (edit.php)
Cancel button - No save, just return to the (list.php) Page
But now image if I can access for edit that item on a different page. How do I return to that calling page?
Do I add a parameter(code) to the URL? something like a Page Origination Code?
Do I save the previous page URL in a session? (bad, they can right click open another page and that would be saved to session url)
Am just curious to how others return to a previous page after a SAVE.
you can the server variable $_SERVER['HTTP_referrer'].
They are other ways also you can store in session the current page and use is processing page.
Adding a parameter to the URL is the only reliable though quote ugly way.
That's why such an in-place editions nowadays often being implemented using AJAX, and this very site is a perfect example.
However, there are different cases.
Login page is imperfect example for example, as you always have a form instead of just a link, and thus you can always store the current page in a hidden form field.
Another approach is possible if you are using some sort of front controller, and all requests actually being directed to the single index.php file which runs appropriate script based on the URI.
in this latter case you will need no more than mere a redirect to the current page.
EXPLAINING WHAT I'M TRYING TO SOLVE:
I have a webpage (file_list.php) showing a list of files, and next to each file there is a button to delete it. When user press the DELETE button close to a certain file name, the browser goes to a script called delete_file.php that deletes the file and then it tells browser to go back to the file_list.php
delete_file.php uses a simple header("Location: file_list.php”); to go back to file_list.php
When browser goes back to file_list.php it reloads the page, but it DOES NOT scroll it back again to where the user was before. So let's say the user scrolled the files list and deleted the last file, when the browser shows again the page file_list.php it won't be scrolled to the bottom of the page again.
THE WORKAROUND I CAME OUT WITH:
I found a strange way to work around this, basically instead of using header("Location: file_list.php”); in delete_file.php I simply use a javascript call window.history.go(-1).
This workaround works perfectly when user is in session (simply using PHP session_start function): the browser RELOADS the file_list.php page and then scrolls it also back to where it was before.
But if the user is NOT in session the browser scrolls the page but IT DOES NOT RELOAD IT before, so the user would still see the file he deleted in the file list.
THE QUESTIONS
Do you know how to reproduce the behavior of the browser when goes back being in session even if we are not in session?
Do you know a way out of this, even another way of solving this matter?
Thanks!
I know I could use AJAX to delete the file so I would not have to go every time to delete_file.php, but this is not the answer.
You could emit anchors:
<a name="anchor1"/>filename_this
<a name="anchor2"/>filename_that
To delete filename_this, you pass the delete page filename_this and also anchor1. The delete page then redirects to file_list.php#anchor1
Note that the anchor names shouldn't map to the file names. That way when you delete the fifth file, the anchor is near the "new" fifth file (where the old one used to be).
Browsers generally remember where you were. If you go from page A to B, then go back (with the back button), you should arrive at the same place in page A.
When you use header('location: A.php');, you are instructing the browser to go forward to another page. It has never been there before, so it can't know what the scroll position was.
When you use history.go(-1), you are instructing the browser to click the back button, which is why your workaround works. You are likely emitting a no-cache header, which is why the browser is reloading the page. The effect here is the same as just reloading the page (without navigating). The trouble with this is that then the user can click the "forward" button, and arrive at your delete_file.php again (and may end up accidentally deleting another file).
Some ideas:
You can put a bunch of anchors on the page (one for each file), and redirect to the anchor that is closest to where the user clicked. So, if you deleted file 4, then redirect to file 3 header('file_list.php#file3');.
Compute the current scroll position in javascript, and store it in a cookie. When the page reloads, use javascript to scroll to where you were (making sure to only do this only once, it would be confusing to visit the page 3 days later and scroll to the middle for no particular reason).
You can also use AJAX to delete the file in the background. Here you are letting the browser deal with the scrolling. This might not be the answer you're looking for, but it's certainly a solution.
In my opinion, the best solution is the simplest: make your pages shorter. If your pages aren't long enough to scroll, then there's no problem.
How about submitting to an IFRAME? (no JS at all? you will not be able to hide the deleted entry...)
So to summarize, your steps are:
1) "a webpage (file_list.php) showing a list of files"
2) "delete_file.php that deletes the file and then it tells browser to go back to the file_list.php"
I'm assuming file_list.php only shows the current contents of the directory, which would obviously not include the file you just deleted. But you want file_list.php to somehow hint to the user something about the file they just deleted, like by jumping back to the position the file would normally be in the list had it still existed. There are probably several dozen ways to do this.
1) delete_file.php opens the url "/file_list.php#the_file_i_just_dele.ted". After you gather directory contents in an array, insert 'the_file_i_just_dele.ted' into that array, probably in alphabetical order or whatever. That array should have another boolean field: "deleted" => 1 | 0, which in case of the deleted file would be 1. While progressing through the array to echo the list, add a html anchor, which could simply be the filename, next to each list item. If 'deleted' is 1, don't echo the 'delete' button next to it. On page open, the page will jump straight to the 'the_file_i_just_dele.ted' anchor.
2) Use ajax.
3) Have a separate php library file for all your filesystem actions. file_list.php is the only page your client needs to see. On delete, file_list.php will post to itself with '#deleted' appended to the url, gather directory contents into an array, then delete the requested file, echo the array, and when your deleted file is encountered in the loop, disable or don't draw the 'delete' button and echo the 'deleted' html anchor right next to it. On page open, the client will automatically jump to the 'deleted' anchor.
4) Make a shopping cart and just not be concerned with this whole 'keeping track of where the user is on the previous page' problem. Have the user check off each file to be deleted. Use a cookie, DB, or session file to keep track of the cart contents if the "shopping" experience would span multiple directories and pages. User clicks a single 'delete' button at the bottom of the page. Add a 'checkout confirmation' page asking the user to confirm deletion of the listed files if you want to.
...etc...
You can also mix and match the solutions as you see fit.