Jump to content

Recommended Posts

Hey all, this is my first message so lets hope this turns out okay.. i have the following bit of php:

[code]    $returnText .= @file_get_contents("loginform.php");[/code]

but the problem is, in the loginform.php i have some php code (as the .php suggests) and its reading everything in that file and just pasting it as is, php bit, html bit, everything. I'm assuming its because I'm using the wrong thing, and that I shouldn't be using file_get_contents, but I don't know what to use. Any help is greatly appreciated!
Link to comment
https://forums.phpfreaks.com/topic/29495-my-little-problem/
Share on other sites

The easiest solution would be an output buffer.

[code]
@ob_start();
include("loginform.php");
$returnText .= @ob_get_contents();
@ob_end_clean();
[/code]

A more difficult (but a little harder) solution would be to rewrite "loginform.php" to not output any text, but actually just store it in a variable. ie, inside loginform.php you would just have:

[code]
$returnText .= 'blah blah blah';
...
$returnText .= 'blah';
...
[/code]

instead of
[code]
echo 'blah blah blah';
...
echo 'blah';
...
[/code]

And then you would just do
[code]
include("loginform.php");
[/code]

from the other file, and it would automatically add all the text to $returnText instead of outputting it.
Link to comment
https://forums.phpfreaks.com/topic/29495-my-little-problem/#findComment-135357
Share on other sites

tried the output buffer, got this:

[code]Fatal error: Cannot redeclare class mysqldb in /home/shearer/inpositionpoker.com/login/database.php on line 14
[/code]

and the part of database.php in which line 14 is in is: (note that line 14 is the line with the first { on it)
[code]class MySQLDB
{
  var $connection;        //The MySQL database connection
  var $num_active_users;  //Number of active users viewing site
  var $num_active_guests;  //Number of active guests viewing site
  var $num_members;        //Number of signed-up users
  /* Note: call getNumMembers() to access $num_members! */

  /* Class constructor */
  function MySQLDB(){
      /* Make connection to database */
      $this->connection = mysql_connect(DB_SERVER, DB_USER, DB_PASS) or die(mysql_error());
      mysql_select_db(DB_NAME, $this->connection) or die(mysql_error());

      /**
      * Only query database to find out number of members
      * when getNumMembers() is called for the first time,
      * until then, default value set.
      */
      $this->num_members = -1;

      if(TRACK_VISITORS){
        /* Calculate number of users at site */
        $this->calcNumActiveUsers();

        /* Calculate number of guests at site */
        $this->calcNumActiveGuests();
      }
  }
[/code]

any other suggestions or a suggestion that fixes this :)
Link to comment
https://forums.phpfreaks.com/topic/29495-my-little-problem/#findComment-135364
Share on other sites

this means that you have already called the mysqldb class. so you are calling the class in 2 files pretty much. if your using the output buffer then remove the line that declares the mysqldb class in loginform.php because it will (i think) use the one from the file you are including it from.
also, i suggest you dont use the @ symbol in front of anything because it will hide all errors. change your php.ini configuration instead.
Link to comment
https://forums.phpfreaks.com/topic/29495-my-little-problem/#findComment-135379
Share on other sites

that what I assumed, so i took out my second include of login/session.php which was including in my oringal file where i did the output buffer thing. but i got another error, so i added a if(!class_exists('MySQLDB')) {    }  bit so that it got rid of my other errors, but now I'm just getting more. I figure I should just post my loginform.php and see if anyone has a better suggestion to fix it. if the include is //'d out it gives me a problem with the value="<? echo $form->value("user"); ?>" part.

[code]<?php
//include("login/session.php");
/**
* User not logged in, display the login form.
* If user has already tried to login, but errors were
* found, display the total number of errors.
* If errors occurred, they will be displayed.
*/
if($form->num_errors > 0){
   echo "<font size=\"2\" color=\"#ff0000\">".$form->num_errors." error(s) found</font>";
}
?>
<form action="login/process.php" method="POST">
<table align="left" border="0" cellspacing="0" cellpadding="3">
<tr><td>Username:</td><td><input type="text" name="user" maxlength="30" value="<? echo $form->value("user"); ?>"></td><td><? echo $form->error("user"); ?></td></tr>
<tr><td>Password:</td><td><input type="password" name="pass" maxlength="30" value="<? echo $form->value("pass"); ?>"></td><td><? echo $form->error("pass"); ?></td></tr>
<tr><td colspan="2" align="left"><input type="checkbox" name="remember" <? if($form->value("remember") != ""){ echo "checked"; } ?>>
<font size="2">Remember me next time &nbsp;&nbsp;&nbsp;&nbsp;
<input type="hidden" name="sublogin" value="1">
<input type="submit" value="Login"></td></tr>
<tr><td colspan="2" align="left"><br><font size="2">[<a href="forgotpass.php">Forgot Password?</a>]</font></td><td align="right"></td></tr>
<tr><td colspan="2" align="left"><br>Not registered? <a href="register.php">Sign-Up!</a></td></tr>
</table>
</form>
[/code]
Link to comment
https://forums.phpfreaks.com/topic/29495-my-little-problem/#findComment-135528
Share on other sites

[quote author=shearer link=topic=117396.msg479131#msg479131 date=1165338002]but i got another error, so i added a if(!class_exists('MySQLDB')) {    }  bit so that it got rid of my other errors
[/quote]

There's a better way to accomplish this than conditionals, simply because conditionals add more lines and confusion to the code.

The function is called "require_once" (or "include_once"). This works very well for including files that only define functions, classes or other things that only need to be defined once.


You can require_once a file at the top of each script, and if a script is included in another one, it won't try to load the file again.

[url=http://us2.php.net/require_once]require_once()[/url]
[url=http://us2.php.net/manual/en/function.include-once.php]include_once()[/url]
Link to comment
https://forums.phpfreaks.com/topic/29495-my-little-problem/#findComment-135720
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.