narcis Posted September 8, 2012 Report Share Posted September 8, 2012 I'm seeing the course PHP SHOPPING CART WITH OOP, MVC & PAYPAL. In the video Template object: part 1 explains how to redirect using a class. I tried to do the same but simplifying the question to be able to understand it better. 1- I create the file "class.php": <?php class Template{ public function redirect($url) { header("location: $url"); exit; } } 2- I create the file index.php where I call the class. I put this in the head: <?php include("class.php"); $Template = new Template(); $Template->redirect('http://www.apple.com'); ?> class.php index.php when I go the the index I'm not redirected. What is missing? Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 8, 2012 Report Share Posted September 8, 2012 Does your "class.php" include an opening <?php tag? And does it include two "}" at the end of the file -- one to close your redirect method, and another to close the class? THat's the thing that I immediately notice. Quote Link to comment Share on other sites More sharing options...
narcis Posted September 8, 2012 Author Report Share Posted September 8, 2012 Yes, "class.php" include the <?php tag and two }. I updated my question and attached the files of my simplified exercise. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 8, 2012 Report Share Posted September 8, 2012 The code you posted works fine for me. What are you getting when you test that page? A blank screen? Error messages? Do you happen to have any white space before your opening <?php tags (that causes errors when using header() )? Do you have error reporting on, so show any PHP errors you might have? If not, try adding ini_set('display_errors',1); error_reporting(E_ALL); to your index.php file after the <?php and see if it reports any issues. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 8, 2012 Report Share Posted September 8, 2012 After downloading your files, I see what your issues is. If you use the exact code you posted in your post above, it will work. However, using the header() function means that nothing (no white space, no code) can be passed to the browser before the redirection happens. Your index.php includes HTML code which needs to be removed for this to work. So your index.php file needs to look like this: <?php include("class.php"); // 3- Crea una instància de la class: // $nomClass = new nomClass(); $Template = new Template(); // 4- Crida la class on la necessita: // $nomClass->nomFunction('recorregut del que vull'); $Template->redirect('http://www.apple.com'); ?> Quote Link to comment Share on other sites More sharing options...
narcis Posted September 8, 2012 Author Report Share Posted September 8, 2012 Ok. I understand. Thank you so much. Narcís Quote Link to comment Share on other sites More sharing options...
falkencreative Posted September 8, 2012 Report Share Posted September 8, 2012 Sure. I hope you are finding the course useful. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.