Page 1 of 2
Execute a link on condition
Posted: Tue Jun 19, 2007 9:40 am
by nano
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.
Posted: Tue Jun 19, 2007 10:19 am
by superdezign
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.
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
}
Posted: Tue Jun 19, 2007 10:48 am
by nano
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
Posted: Tue Jun 19, 2007 11:12 am
by superdezign
I don't think sessions send headers.. do they? I think you've got output elsewhere. Make sure this at the the start of the page, right after you start the session.
Posted: Tue Jun 19, 2007 4:07 pm
by feyd
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.
Posted: Tue Jun 19, 2007 4:20 pm
by superdezign
feyd 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.
My code was wrote under the assumption that the filename was an absolute path. :-p
Maybe you could try using the output buffer. No idea what that'd do for a session, but it's worth a shot.
Posted: Tue Jun 19, 2007 4:21 pm
by feyd
Absolute path is not the same as full URL.

Posted: Tue Jun 19, 2007 5:04 pm
by superdezign
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.
Posted: Tue Jun 19, 2007 5:09 pm
by feyd
superdezign wrote:You can't open remote files with fopen.
Since when?

Posted: Tue Jun 19, 2007 6:16 pm
by superdezign
Don't you dare confuse me!
I was under the impression that you needed to use cURL or file_get_contents() to open remote files.
Posted: Tue Jun 19, 2007 7:37 pm
by feyd
superdezign wrote:I was under the impression that you needed to use cURL or file_get_contents() to open remote files.
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.
Posted: Tue Jun 19, 2007 7:51 pm
by superdezign
Ohh.

Posted: Tue Jun 19, 2007 10:00 pm
by nano
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).
Posted: Tue Jun 19, 2007 10:05 pm
by feyd
nano wrote: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?
Try it.
Posted: Tue Jun 19, 2007 10:07 pm
by nano
nano wrote:So if i try to redirect after that i just get:
Warning: Cannot modify header information - headers already sent by....etc
Like i said before, i have. What i was asking was is there a
different way to do it because it wont work in the way im currently trying.