Jump to content

class redirect


narcis

Recommended Posts

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...