1.11M Members

help echoing out a url

 
0
 
$url = $_GET['url'];
echo $url;

i dont know why i get a Undefined index error

 
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;
 
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

 
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'; 
    }
}

?>
 
0
 

Thanks it works now

 
0
 

is there anything wrong with my code ?

 
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

 
0
 

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

 
0
 

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

 
0
 

which has the require statement

 
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

 
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.

 
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

 
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.

 
0
 

heres my .htaccess file


RewriteEngine On

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

 
0
 

i do specify index.php

 
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

 
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