PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,149 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,689 people online right now. Registration is fast and FREE... Join Now!




Loading a page in PHP

 

Loading a page in PHP

Zel2008

4 Nov, 2009 - 08:50 AM
Post #1

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Hi everybody,
I know of one way to load a page in PHP:
header( "Location: page.php" );

But my question is slightly different. Is there a way to load a new page based on a function call? Basically something like this:
CODE

                if( $one === $two ) {
                    echo "<pre>$one</pre>";
                } else {
                    OtherClass::showNewPage( $two );
                }


The showNewPage function creates a completely new page, with content determined by $two. So, at the time the function is called, a particular page is showing, and after the function has been called, the new page it makes should display.

Does anyone know how to do this? I'd appreciate any advice.

Thanks,
Zel2008

User is offlineProfile CardPM
+Quote Post


CTphpnwb

RE: Loading A Page In PHP

4 Nov, 2009 - 05:26 PM
Post #2

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
You can include a file or you can use header using a variable.
$x = "somefile.php";
include($x);
$x = "Location: page.php";
header($x);
User is offlineProfile CardPM
+Quote Post

ShaneK

RE: Loading A Page In PHP

5 Nov, 2009 - 04:43 AM
Post #3

require_once("brain.php"); //Fatal error :/
Group Icon

Joined: 10 May, 2009
Posts: 701



Thanked: 47 times
Dream Kudos: 75
Expert In: PHP, MySQL

My Contributions
You can also use meta tags and Javascript. I personally prefer meta tags over headers because they aren't required to be the first output thing for them to still work.

Yours,
Shane~
User is offlineProfile CardPM
+Quote Post

Zel2008

RE: Loading A Page In PHP

5 Nov, 2009 - 05:35 AM
Post #4

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Thanks guys,

At the risk of sounding stupid, I don't understand how your solutions apply. I know how you can include files, but how does that make it so a completely new page will be generated after a function call completes? The showNewPage function in my example doesn't load an existing php page, it creates a new one on the fly with echo statements.

Thanks,
Zel2008

This post has been edited by Zel2008: 5 Nov, 2009 - 05:36 AM
User is offlineProfile CardPM
+Quote Post

CTphpnwb

RE: Loading A Page In PHP

5 Nov, 2009 - 05:45 AM
Post #5

D.I.C Lover
Group Icon

Joined: 8 Aug, 2008
Posts: 2,101



Thanked: 155 times
Dream Kudos: 100
Expert In: PHP

My Contributions
A) Since you don't show the function, there's no way we can know what it does.

cool.gif If it does what you say, then where's the problem? You give $two the appropriate value and the page you're looking for is generated!
User is offlineProfile CardPM
+Quote Post

Zel2008

RE: Loading A Page In PHP

5 Nov, 2009 - 06:38 AM
Post #6

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Sorry I wasn't more specific, here's a test case that shows this. The test starts from StartPage.php:

CODE

<?php

echo "<HTML><HEAD><TITLE>Test</TITLE></HEAD>\n";
echo "<BODY>\n";

echo "<FORM ACTION=TestRun.php METHOD=\"POST\"\n";
echo "<INPUT TYPE=\"TEXT\" NAME=\"TextTest\">\n";
echo "<INPUT TYPE=\"Submit\" NAME=\"Submit\" VALUE=\"Submit\"\n";
echo "</FORM>\n";

echo "</BODY></HTML>";

?>


and the form goes to TestRun.php:
CODE

<?php
include ( "ErrorTest.php" );

echo "<HTML><HEAD><TITLE>Runner</TITLE></HEAD>\n";
echo "<BODY></P>Running...</P></BODY></HTML>\n";

$length = strlen( $_POST["TextTest"] );
if( $length > 0 ) { echo "OK"; }
else { ErrorTest::makeError( "This is an error." ); }

?>


and if an error happens, the code in ErrorTest.php should run:
CODE

<?php

class ErrorTest {
  public static function makeError( $value ) {
    echo "<HTML><HEAD><TITLE>Error</TITLE></HEAD>\n";
    echo "<BODY><P>ErrorPage</P></BODY></HTML>"
  }
}

?>


For some reason, the include statement in RunTest.php doesn't quite seem to work, but the problem still shows up anyway. The html code generated by the method in ErrorTest shows up on the same page as the one generated by RunTest; calling the ErrorTest method doesn't create an entirely new page, as I'd like it to.

So the output of running this, with an error, is:
CODE

<-- One Page-->
<HTML><HEAD><TITLE>Runner</TITLE></HEAD>
<BODY></P>Running...</P></BODY></HTML>

<HTML><HEAD><TITLE>Error</TITLE></HEAD>
<BODY><P>ErrorPage</P></BODY></HTML>


when it should be:
CODE

<--Run Page-->
<HTML><HEAD><TITLE>Runner</TITLE></HEAD>
<BODY></P>Running...</P></BODY></HTML>
<--Error Page (New Page)-->
<HTML><HEAD><TITLE>Error</TITLE></HEAD>
<BODY><P>ErrorPage</P></BODY></HTML>


Is that clearer?

Thanks,
Zel2008

This post has been edited by Zel2008: 5 Nov, 2009 - 06:41 AM
User is offlineProfile CardPM
+Quote Post

RPGonzo

RE: Loading A Page In PHP

5 Nov, 2009 - 07:53 AM
Post #7

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
The header function like CT stated would be used ... unless you dont want the location of the user to change ...

if you want the page they are on now to show what you what when you want maybe look into ouput buffering...

http://www.php.net/manual/en/function.ob-start.php

if data is already sent to the browser a meta refresh would be inline to redirect the user with no errors to your error page ..

small example of output buffering ( im not the best at this so this is a EXAMPLE )
CODE

<?php
error_reporting(E_ALL);

$error = false;

ob_start();
echo "this is some text for our normal page";
if ($error) {
    ob_end_clean();
    echo "an error occured please try back later";
    exit;
}
ob_end_flush();
?>


change $error to true and see how it works have fun smile.gif
User is offlineProfile CardPM
+Quote Post

Zel2008

RE: Loading A Page In PHP

5 Nov, 2009 - 08:12 AM
Post #8

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Thanks RPGonzo,
That was beautiful, it worked! Thank you!

I have one quick question, though--I have a processing page that pops up just before this, and in order to get that to work and still be flexible, I have to use a system call:

CODE

system( "php Processing.php" );


I learned (from you, thanks again) that you shouldn't use header() if you want a page to have the ability to change--so I didn't use it--but I was wondering, is this the proper alternative or is there a better way?

Thanks again,
Zel2008

This post has been edited by Zel2008: 5 Nov, 2009 - 08:13 AM
User is offlineProfile CardPM
+Quote Post

RPGonzo

RE: Loading A Page In PHP

5 Nov, 2009 - 08:21 AM
Post #9

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
header has many different uses besides changing page locations .. so don't go hating it just yet wink2.gif

your processing page ... im curious as to WHAT you use this for ... just a loading background .. or a redirect platform ... or ????
User is offlineProfile CardPM
+Quote Post

Zel2008

RE: Loading A Page In PHP

5 Nov, 2009 - 08:28 AM
Post #10

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Hi RPGonzo,
The processing page is just a loading background, it basically just says, "Your job is running, please wait" and then goes away when the job is done and results pop up.

You're right about header(), I have to admit. It does do some good things for me in my code, so I don't detest it too much yet. There's more to do in my project, though, so we'll see. smile.gif
Thanks,
Zel2008
User is offlineProfile CardPM
+Quote Post

RPGonzo

RE: Loading A Page In PHP

5 Nov, 2009 - 09:30 AM
Post #11

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
i don't know if im fully 100% understanding WHAT your trying to accomplish but maybe i got close with this example ...

CODE

<?php
error_reporting(E_ALL);

if (isset($_POST['submit']) && $_POST['input'] != "") {
    if (isset($_POST['input']) && $_POST['input'] <= "3") {
        switch ($_POST['input']) {
            case 1:
                $error = "this is the first error";
             break;
            case 2:
                $error = "this is the second error";
             break;
            case 3:
                $error = "this is the third error";
             break;
            default:
                $error = NULL;
             break;
        }
    } else {
        header("Refresh: 3;" . $_SERVER['PHP_SELF']);
        echo "please wait submitting data";
        exit;
    }
} else if (isset($_POST['submit']) && $_POST['input'] == "") {
    echo "value cannot be empty.<br/>";
}

ob_start();
echo "this is some text for our normal page";
if (isset($error) && $error != NULL) {
    ob_end_clean();
    echo "an error occured: $error";
    exit;
}
ob_end_flush();
?>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
    <input type="text" value="" name="input" size="2"/>
    <input type="submit" value="Simulate Submit" name="submit"/>
</form>


testing just by hitting the submit ... when you want to "simulate" a error values 1-3 produce errors anything else processes and blank triggers blank notice

maybe im close crazy.gif maybe im not tongue.gif sleep.gif

note:
now there are a couple of draw backs doing it this way ... of course cause we are using a refresh the post contents can be resent with a browser refresh ( which i hate lol ) and ALL your error checking HAS to be done BEFORE anything even TRIES to be outputted which should be mandatory in my opinion anyways ...



This post has been edited by RPGonzo: 5 Nov, 2009 - 09:31 AM
User is offlineProfile CardPM
+Quote Post

Zel2008

RE: Loading A Page In PHP

5 Nov, 2009 - 12:10 PM
Post #12

D.I.C Head
**

Joined: 6 Jan, 2009
Posts: 173


My Contributions
Hi RPGonzo,
No, that's not what I was trying to do, but I was able to use the example you gave before, tweaked a bit, for my needs, so thanks again.
Thanks,
Zel2008
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:29PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month