include('$file');

Discussion in 'PHP' started by promotingspace.net, Jun 27, 2007.

  1. #1
    Hi
    Can I do this:
    $file="articledetails.php";
    include('$file');
    I really need it or something similar to make my pages more dynamic
     
    promotingspace.net, Jun 27, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    variables won't be parsed between single quotes.

    
    include($file);
    
    PHP:
     
    nico_swd, Jun 27, 2007 IP
  3. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    solved! thanks
     
    promotingspace.net, Jun 27, 2007 IP
  4. tamilsoft

    tamilsoft Banned

    Messages:
    1,155
    Likes Received:
    78
    Best Answers:
    0
    Trophy Points:
    0
    #4
    You can also use like this: include("$file");
     
    tamilsoft, Jun 27, 2007 IP
  5. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    now i'm using this code as the next step:
    function membersOnly() {
            error_reporting(E_ALL);
            ini_set('display_errors', true);
           
            if (!$_SESSION['logged']) {
                    $_SESSION['log_to'] = $_SERVER['REQUEST_URI'];
                    session_write_close();
                {   $file="login.php";
                     header('Location: message.php'); }
                    die();
            }
    }
    PHP:
    and message.php has the following code on line 67:
    <?  include "$file"; ?>
    PHP:
    now it is redircted to message.php but the login form is not shown in it. instead it prompts:
    Notice: Undefined variable: file in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\message.php on line 67

    Warning: main(): Failed opening '' for inclusion (include_path='.;G:/Programs(2)/EasyPHP1-8\php\pear\') in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\message.php on line 67
     
    promotingspace.net, Jun 27, 2007 IP
  6. sm9ai

    sm9ai Active Member

    Messages:
    746
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    60
    #6
    Once you have moved page $file will have not be defined.

    store the value of $file in a session variable or include it at the end of your message. You might need to call session_start depending upon your setup e.g.

    in function membersOnly() add

    session_start(); 
    $_SESSION['file'] = "login.php"; 
    PHP:
    and in message.php
    <?  include "$_SESSION['file']"; ?>
    PHP:
    although I'm not sure if you can include a session variable you might need to put it into a local variable first e.g.

    <? 
    $file=$_SESSION['file']
    include "$file";
    ?>
    PHP:
     
    sm9ai, Jun 28, 2007 IP
  7. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #7
    You can include files in session variables. But your example would throw a parse error. Just remove the double quotes or put the variable between curly brackets.
     
    nico_swd, Jun 28, 2007 IP
  8. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thanks for your reply
    I tried it now but it seem it's not working. it prompts:
    Notice: Undefined variable: _SESSION in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\message.php on line 67

    Warning: main(): Failed opening '' for inclusion (include_path='.;G:/Programs(2)/EasyPHP1-8\php\pear\') in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\message.php on line 67
     
    promotingspace.net, Jun 28, 2007 IP
  9. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    that threw a parse error too. someone suggested I use include instead of redirect. i'm gonna try it
     
    promotingspace.net, Jun 28, 2007 IP
  10. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #10
    You need to add session_start() at the top of your page to access $_SESSION variables.

    And both ways I said would work.
    
    include "{$_SESSION['file']}";
    // Or
    include $_SESSION['file'];
    
    PHP:
     
    nico_swd, Jun 28, 2007 IP
  11. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Hi
    this code solved it:
    function membersOnly() {
            error_reporting(E_ALL);
            ini_set('display_errors', true);
           
            if (!$_SESSION['logged']) {
                    $_SESSION['log_to'] = $_SERVER['REQUEST_URI'];
                    session_write_close();
                session_start(); 
                    $_SESSION['file'] = "login.php";
                    include 'message.php';
                                die();
            }
    }
    PHP:
    and in message.php:
    <?  include $_SESSION['file']; ?>
    PHP:
    it shows the login form now. but it prompts another error upon login:
    Fatal error: Call to a member function on a non-object in g:\programs(2)\easyphp1-8\www\ha\previous\new folder\htdocs\login.php on line 36
    this error does not occur when I use login.php alone for logging in.
    line 36 begins as:
    	if ($user->_checkLogin($processed['username'], $processed['password'], $remember)) {
    			if (isset($_SESSION['log_to'])) {
    				redirect($_SESSION['log_to']);
    			} else {
    				redirect('account.php/');
    			}
    		} else {
    			failed($form);
    		}
    PHP:
     
    promotingspace.net, Jun 28, 2007 IP
  12. promotingspace.net

    promotingspace.net Peon

    Messages:
    361
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    by the way,
    here:
    $db = db_connect();
    $user = new User($db);
    PHP:
    User is a class with over 260 lines. there, the function checklogin:
    function _checkLogin($username, $password, $remember) {
    		$md5pass = "'" . md5(substr($password, 1, -1)) . "'";
    		$sql = "SELECT * FROM member WHERE " .
    			"(username = $username) AND " .
    			"(password = $md5pass) AND " .
    			"(permission != '00-deny')";
    
    		$result = $this->db->getRow($sql);
    
    		if (is_object($result)) {
    			$this->_setSession($result, $remember);
    			return true;
    		} else {
    			$_SESSION['login']--;
    			$this->_logout();
    			return false;
    		}
    	}
    PHP:
     
    promotingspace.net, Jun 28, 2007 IP
  13. Acecool

    Acecool Peon

    Messages:
    267
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Avoid using double quotes where you can.. They take longer to parse because everything is looked at, with single it is what it is.. nothing else needs to be done.
     
    Acecool, Jun 28, 2007 IP