Execute a link on condition
Moderator: General Moderators
Execute a link on condition
Ok i'm just really stumped as to how to word my problem to come up with an answer from google. I've been coding php for about a year now and php.net has never failed me until now and that's just because i have no idea what to search for.
What i'm trying to do is this:
My php script will attempt to open the file if it exists, or it will write the file then open it. Basically i want a redirect function that will happen if a condition is met (file exists).
If it makes a difference, i'm writing an XML file and then trying to open it.
Any help would be appreciated. Javascript is also fine if necessary.
Thanks.
What i'm trying to do is this:
My php script will attempt to open the file if it exists, or it will write the file then open it. Basically i want a redirect function that will happen if a condition is met (file exists).
If it makes a difference, i'm writing an XML file and then trying to open it.
Any help would be appreciated. Javascript is also fine if necessary.
Thanks.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
Your making it and opening it in the same file?
If you are using fopen() to open/create the file, you can assume that if that function succeeded, you can open the file immediately afterwards.
If you are using fopen() to open/create the file, you can assume that if that function succeeded, you can open the file immediately afterwards.
Code: Select all
if($fh = @fopen($filename))
{
// Create the file with fwrite and such
header('Location: ' . $filename);
}
else
{
// The file could not be opened/created
}Yeah i would have used that redirect function if i could have, but my script checks/creates a session to make sure the user is logged in when trying to access the page. The user must be logged in because the script generates the XML document based on the users preferences. So if i try to redirect after that i just get:
Warning: Cannot modify header information - headers already sent by....etc
Warning: Cannot modify header information - headers already sent by....etc
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
My code was wrote under the assumption that the filename was an absolute path. :-pfeyd wrote:Starting sessions sets headers.
For a bit of clarification on header() based redirection, you really need to use a full URL. HTTP standard requires it and some browsers are quite strict in their following of standards.
Maybe you could try using the output buffer. No idea what that'd do for a session, but it's worth a shot.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
feyd wrote:Absolute path is not the same as full URL.
True. I'm used to all of my website defining a ROOTPATH constant that refers to my website's URL. I didn't even put into consideration that not everyone does it.
I mean.. uh.. my code assumed that a full URL is given as the filename... no... that wouldn't make sense. You can't open remote files with fopen.
- superdezign
- DevNet Master
- Posts: 4135
- Joined: Sat Jan 20, 2007 11:06 pm
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
Your impression is partly true. fsockopen() and fopen() can also open remote files. fopen() along with file_get_contents() requires allow_url_fopen on in the PHP configuration to access this ability.superdezign wrote:I was under the impression that you needed to use cURL or file_get_contents() to open remote files.
So back to the original question, if i need it to redirect after i have checked for/created a session, is that possible to do?
Or would i have to get all the user preferences from the previous page and post them through to the page that creates the file? (so that i dont have to check sessions at all)
I can see that this second method would work but it'd be very inefficient code as i'd need to have that code on every single page that links to the script which creates the file (which is every page on the site).
Or would i have to get all the user preferences from the previous page and post them through to the page that creates the file? (so that i dont have to check sessions at all)
I can see that this second method would work but it'd be very inefficient code as i'd need to have that code on every single page that links to the script which creates the file (which is every page on the site).