Execute a link on condition

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

nano
Forum Newbie
Posts: 8
Joined: Tue Jun 19, 2007 9:31 am

Execute a link on condition

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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
}
nano
Forum Newbie
Posts: 8
Joined: Tue Jun 19, 2007 9:31 am

Post 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
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Absolute path is not the same as full URL. ;)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

feyd wrote:Absolute path is not the same as full URL. ;)
8O

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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

superdezign wrote:You can't open remote files with fopen.
Since when? ;)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Don't you dare confuse me! :lol:

I was under the impression that you needed to use cURL or file_get_contents() to open remote files.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post by superdezign »

Ohh. :D
nano
Forum Newbie
Posts: 8
Joined: Tue Jun 19, 2007 9:31 am

Post 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).
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
nano
Forum Newbie
Posts: 8
Joined: Tue Jun 19, 2007 9:31 am

Post 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.
Post Reply