1,105,290 Community Members

help echoing out a url

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 
$url = $_GET['url'];
echo $url;

i dont know why i get a Undefined index error

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,831 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

You have to provide ?url= in the address bar. If that is missing, you get the index error. You can change it to this:

$url = isset($_GET['url']) ? $_GET['url'] : '<no url set>';
echo $url;
Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

hi thanks for the help, it has cleared the errors but its not echoing out the url ? ive been following a php tutorial and i dont how he has got it working

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,831 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

How do you call your script ? http://localhost/script.php?url=myurl

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

my index.php

<?php

$url = isset($_GET['url']) ? $_GET['url']: '<no url set>';
echo $url;


require 'controllers/' .$url . '.php';

$controller = new $url;





?>

my controller :

<?php
class Index {
    
    function __construct() {
        
        echo 'we are in index'; 
    }
}

?>
Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,831 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 
Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Thanks it works now

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

is there anything wrong with my code ?

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Warning: require(C:\wamp\www\mvc\controllers) [function.require]: failed to open stream: Permission denied in C:\wamp\www\mvc\index.php on line 7

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,831 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

What did you do? Pasting an error without showing your code is kinda useless.

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

oh i i did not do nothing i just went to my index.php,

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

which has the require statement

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

( ! ) Notice: Undefined index: url in C:\wamp\www\mvc\index.php on line 3
Call Stack
# Time Memory Function Location
1 0.0007 364448 {main}( ) ..\index.php:0

( ! ) Warning: require(C:\wamp\www\mvc\controllers) [function.require]: failed to open stream: Permission denied in C:\wamp\www\mvc\index.php on line 7
Call Stack
# Time Memory Function Location
1 0.0007 364448 {main}( ) ..\index.php:0

( ! ) Fatal error: require() [function.require]: Failed opening required 'controllers/' (include_path='.;C:\php\pear') in C:\wamp\www\mvc\index.php on line 7
Call Stack
# Time Memory Function Location
1 0.0007 364448 {main}( ) ..\index.php:0

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,831 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

Show your code, not the call stack. Although the last error says the file does not exist, so you need to check that before you include it.

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

its the same code i showed you at the start here is :

index.php

<?php

$url = $_GET["url"];
echo $url;


require 'controllers/' .$url;

$controller = new $url;



?>

index.php in the folder called controller:

<?php
class Index {
    
    function __construct() {
        
        echo 'we are in index'; 
    }
}

?>

that is all i have

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,831 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

If there is no url specified, the require will fail, that is why you have those messages. You will have to call it as I specified earlier, or add error checking.

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

heres my .htaccess file


RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

i do specify index.php

Member Avatar
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

http://localhost:8080/mvc/index.php?url=Index

it echos out index

and these error messages


( ! ) Warning: require(controllers/Index) [function.require]: failed to open stream: No such file or directory in C:\wamp\www\mvc\index.php on line 7
Call Stack
# Time Memory Function Location
1 0.0005 365256 {main}( ) ..\index.php:0

( ! ) Fatal error: require() [function.require]: Failed opening required 'controllers/Index' (include_path='.;C:\php\pear') in C:\wamp\www\mvc\index.php on line 7
Call Stack
# Time Memory Function Location
1 0.0005 365256 {main}( ) ..\index.php:0

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,831 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
1
 

The problem may be:

require 'controllers/' .$url; //  this should open controllers/index.php
$controller = new $url; // this should be Index (the class name)

The following could help:

require 'controllers/' . strtolower($url) . '.php';
$controller = new $url;

If you want to open index.php?url=Index

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article