Better PHP/HTML Layout to Handle Refreshes
Moderator: General Moderators
-
JonOfAllTrades
- Forum Newbie
- Posts: 6
- Joined: Mon Nov 07, 2005 3:47 pm
- Location: Clarksville, TN
Better PHP/HTML Layout to Handle Refreshes
Hello and good morning, peeps.
I'm new to PHP, and I've been cheerfully whipping out login and admin pages, so far so good. When a user submits an action with a link or button, the page is reloaded with a $_GET['Action'] value. The start of each page checks for this and handles it. This lets me keep all my display code and my action code in the same files. HOWEVER, a user that hits Refresh can duplicate an action. In practice, most actions are safely repeatable (there's a technical term for that), because submitting the same data twice will bump into a unique key in MySQL and get ignored, but still. It's inelegant and potentially unsafe.
Would anyone like to show this newbie how it's done? I don't want to get too complicated. Would it make sense to put action code in a separate file, and then have it include the main page code?
Thank you!
I'm new to PHP, and I've been cheerfully whipping out login and admin pages, so far so good. When a user submits an action with a link or button, the page is reloaded with a $_GET['Action'] value. The start of each page checks for this and handles it. This lets me keep all my display code and my action code in the same files. HOWEVER, a user that hits Refresh can duplicate an action. In practice, most actions are safely repeatable (there's a technical term for that), because submitting the same data twice will bump into a unique key in MySQL and get ignored, but still. It's inelegant and potentially unsafe.
Would anyone like to show this newbie how it's done? I don't want to get too complicated. Would it make sense to put action code in a separate file, and then have it include the main page code?
Thank you!
Hmm.
Once the form has been actioned, have bounce them back to the same page again, but without any values (except maybe one to say 'yay, your login is accepted, or something).
This is the wrong forum for this sort of stuff, by the way.
Once the form has been actioned, have
Code: Select all
header ("thispage.php");
exit;This is the wrong forum for this sort of stuff, by the way.
-
JonOfAllTrades
- Forum Newbie
- Posts: 6
- Joined: Mon Nov 07, 2005 3:47 pm
- Location: Clarksville, TN
Sorry. Interface design forum? Can I move it?Grim... wrote:Hmm.
Once the form has been actioned, havebounce them back to the same page again, but without any values (except maybe one to say 'yay, your login is accepted, or something).Code: Select all
header ("thispage.php"); exit;
This is the wrong forum for this sort of stuff, by the way.
That won't work in my case; I should explain further.
The only page anyone accesses on my server is Blueprint.php, which lays out a title, links, a table layout, handles some javascript, etc. It include()'s the actual document requested, as specified in a GET and verified from a list of valid Docs. By the time an individual document is opened, quite a bit of HTML has already been posted.
On a slightly separate note, is there a simple way to break out of a file? Whenever a user requests a secure page, the first thing it does is check for a valid session, and if not, it include()'s the appropriate login page instead. As a result, the whole file is in an else bracket.
UserAdminHome.php
Code: Select all
if (array_key_exists('User' , $_SESSION) == false) // I know, this needs to be expanded
{
echo("<p class='Warning'>You are not logged in.</p>\n");
include("StaffLogin.php");
}
else
{
...entire document lies in a bracket
}-
JonOfAllTrades
- Forum Newbie
- Posts: 6
- Joined: Mon Nov 07, 2005 3:47 pm
- Location: Clarksville, TN
Hmm. I thought you could not use header() once you'd outputted any part of the document?Grim... wrote:You can use header() to jump back into the same page, but with different variables.
So it will work for the first problem.
As for the break thing, I think you're looking for exit()
Exit() drops out of PHP, cutting the document off. That won't work with the architecture I have:
GET: Blueprint.php?Doc=StaffLogin
Blueprint.php:
Code: Select all
<?php session_start(); ?>
...header stuff, confirms $_GET['Doc'] is valid, output title
...javascript functions
<table ...>
...various layout elements
...title, random subtitle, etc
include($ContentFiles[$Document]);
...random picture, links from separate txt file, boilerplate, etc.
</table>The layout is reasonably liquid (you can see it at 209.159.34.53 if you like, comments welcome), but I need the table layout to make it work.
I suppose I could break the Blueprint.php template into two parts, and have every document call include("Blueprint1.php") and include("Blueprint2.php") with the page-specific code in the middle, but I would still need to enclose the whole meat of the file in a block:
UserAdminHome.php:
Code: Select all
include("Blueprint1.php");
if(/* logged in */)
{
// Display fun stuff
}
else
{
include("StaffLogin.php"); // Oops, this file will include() Blueprint1 and 2 again; maybe include_once()?
}
include("Blueprint2.php");I appreciate the suggestions! Any ideas on how to rearrange things without having to have my layout code in every file?
-
JonOfAllTrades
- Forum Newbie
- Posts: 6
- Joined: Mon Nov 07, 2005 3:47 pm
- Location: Clarksville, TN
it's actually header('Location:http://full.url.here');
you should put the full url for compatibility reasons. older browsers could choke on a relative url
edit: heh this isnt the first time people've got confused by alias functions, there's quite a few of them and I've seen people arguing over which one to use (count() and sizeof())
you should put the full url for compatibility reasons. older browsers could choke on a relative url
edit: heh this isnt the first time people've got confused by alias functions, there's quite a few of them and I've seen people arguing over which one to use (count() and sizeof())
So it is.jshpro2 wrote:it's actually header('Location:http://full.url.here');