We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,134 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

help echoing out a url

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

i dont know why i get a Undefined index error

2
Contributors
20
Replies
1 Day
Discussion Span
1 Year Ago
Last Updated
21
Views
Question
Answered
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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;
pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

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

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

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

?>
moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

Thanks it works now

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

is there anything wrong with my code ?

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

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

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

which has the require statement

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

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

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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.

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

heres my .htaccess file


RewriteEngine On

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

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

i do specify index.php

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 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

moroccanplaya
Junior Poster
178 posts since May 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

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

pritaeas
Posting Prodigy
Moderator
9,293 posts since Jul 2006
Reputation Points: 1,178
Solved Threads: 1,462
Skill Endorsements: 86

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
 
© 2013 DaniWeb® LLC
Page rendered in 0.1406 seconds using 2.72MB