Results 1 to 7 of 7

Thread: error include url

  1. #1
    Join Date
    Feb 2008
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default error include url

    i have two problem with url in my code . Example :
    Code:
    <?PHP if (empty($_GET[page])) $_GET[page] = home; include "$svr_rootself/$_GET[page].php"; ?>
    with this method my url is www.example.com/?page=example&ex=2&exe=3 . ( example=example.php in server root and worked 100% ) now when change manually url to ?page=example.php i have two error .


    Code:
    Warning: include(htdocs/site/example.php.php) [function.include]: failed to open stream: No such file or directory in htdocs\site\index.php on line 49
    
    Warning: include() [function.include]: Failed opening 'htdocs/site/detail.php.php' for inclusion (include_path='.;\php\pear\') in site\index.php on line 49
    how to fix this error ? regards

  2. #2
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Try the below mentioned code for your purpose:
    Code:
    <?php 
    	if (empty($_GET['page'])) {
    		$_GET['page'] = "home"; 
    	}
    	if(!strripos($_GET['page'],".php"))
    		include "$svr_rootself/$_GET['page'].php"; 		
    	else
    		include "$svr_rootself/$_GET['page']";
    ?>
    The error reported occurred for you because your PHP code appends a ".php" after the value stored in $_GET['page'] so if you pass "example.php" as its value then it will append ".php" after that and will result in "example.php.php".

    The above furnished code checks whether it contains an extension if so it will not append ".php" on it.

    I don't have any idea about the value stored in $svr_rootself variable and so it is difficult for me to tell whether it includes the correct path.

    Hope this helps.
    Last edited by codeexploiter; 07-23-2008 at 11:28 AM.

  3. #3
    Join Date
    Feb 2008
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    thanks ! but i try this code . send this error
    Code:
    Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in \htdocs\site\index.php on line 54

  4. #4
    Join Date
    Feb 2008
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    any idea for fix this error ??

  5. #5
    Join Date
    Sep 2005
    Location
    India
    Posts
    1,627
    Thanks
    6
    Thanked 107 Times in 107 Posts

    Default

    Plz try the following code:
    Code:
    <?php 
    	$page = $_GET['page'];
    	if (empty($page)) {
    		$page = "home"; 
    	}
    	if(!strripos($page,".php"))
    		include "$svr_rootself/$page.php"; 				
    	else
    		echo "$svr_rootself/$page";	
    ?>
    I've used a variable to hold the value of page name in this code. As I've mentioned I don't know the value stored in $svr_rootself variable make sure it contains correct value.

  6. #6
    Join Date
    Feb 2008
    Posts
    62
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    thanks ! this code worked WITHOUT ERROR But Show This Line Code htdocs/dir/browse.php just this line . my idea how to direct to any error page ! example : mysite.com/index.php?page=error ( this page not found ) or redirect to index.php or page=example thanks man , regards
    Last edited by why not; 07-24-2008 at 09:06 AM.

  7. #7
    Join Date
    Oct 2006
    Posts
    183
    Thanks
    0
    Thanked 11 Times in 11 Posts

    Default

    If you mean you want it to redirect to page=error if it doesn't exist, try this:

    Code:
    <?php 
    	$page = $_GET['page'];
    	if (empty($page)) {
    		$page = "home"; 
    	}
    	if(!strripos($page,".php"))
    		include "$svr_rootself/$page.php"; 				
    	else
    		header("Location: ./index.php?page=error&ref=$svr_rootself/$page");	
    ?>
    Then put this into your error.php file wherever you want it to show what page caused the error:
    Code:
    echo $_GET['ref'];
    Optionally just use this to only redirect to an error page without telling the page that caused it:
    Code:
    <?php 
    	$page = $_GET['page'];
    	if (empty($page)) {
    		$page = "home"; 
    	}
    	if(!strripos($page,".php"))
    		include "$svr_rootself/$page.php"; 				
    	else
    		header("Location: ./index.php?page=error");	
    ?>

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •