I'm developing a system where users need to make an account, and once the account is fully created it should take them to the home page, for this I use the PHP tag header.
My code looks like this:
private function redirect($location) {
if(!isset($location)) { $location = '/'; }
header('Location: ' . $location);
}
For some reason when I execute this without specifying $location or with anything it's taking me to (in the url bar)
/var/www/html/our-site-name/<$location>
Does anyone know what might be the issue here?
say you have a directory structure like this
<wwwroot>
<login>
<signup>
index.php
then your function should look like this if it is run from the signup folder
private function redirect($location) {
if(!isset($location)) { $location = './index.php'; }
header('Location:'.$location);
}
"./" takes you up 1 directory
Related
I have a redirect script which does some tracking and then redirects the user to a destination. It looks something like this
class Redirect() {
private function init() {
// analyze parameters
(...)
$this->referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$this->trackVisit($this->referer, $someMoreData);
$destination = $this->getUrlByParameters(...);
$this->redirectUrl = $destination;
}
private function run() {
(...)
header('Location: ' . $this->redirectUrl);
(...)
}
}
$r = new Redirect();
if($r->init()) {
$r->run();
}
What gives me headache is that i can see a referrer on my server and i am saving that in to my db but after redirecting the user it disappears. The destination and all possible subsequent redirects don't have the referrer anymore.
Referrer-Policy is set to 'no-referrer-when-downgrade' but i am always redirect to https so that should not be the issue.
I hope somebody can help me out!
If you redirect the request from a script using header("Location:...") The Å•eferrer will not be preserved. You could possibly pass the referer as a variable in query string:
header('Location: ' . $this->redirectUrl."?ref=".$_SERVER['HTTP_REFERER']);
or with &, if the URL already contains some parameters.
Then you could get the original referer on the other script using $_GET["ref"]
Or you could write headers yourself and append it before the Location header, and then send full headers.
i have this piece of code, saved inside a file called 'functions.php'
$host = $_SERVER['HTTP_HOST'];
$folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
function redirect_to_home(){ /*function to redirect the user ot his profile page, or admin page if he is an administrator*/
if(admin_class::isadmin()){
header("Location:http://$host$folder//adminindex.php");
exit();
}
else{
header("Location:http://$host$folder/profile.php");
exit();
}
}
function redirect_to_welcome(){
header("Location:http://$host$folder/index.php");
exit;
}
function redirect_to_loan(){
header("Location:http://$host$folder/loan.php");
exit;
}
When browsing through the website itself, these don't work correctly and I could only navigate via links within the website, I've finished the whole thing though (I just used header("Location:http://localhost/YMMLS//pagename.php") when I was developing).
And I need some enlightenment here, I'm launching this website via LAN and those connected could access it via xxx.xxx.xxx.x/YMMLS. But of course, the website fails to redirect properly when any of these functions are called.
Thanks in advance,
NetBeans is warning you, because the variables are declared outside the function.
They don't exist within the scope of the function. Sure, you can call the function -- from outside the function, where those variables do exist -- but that's different. :)
Here -- try this;
class redirect {
public $host = '';
public $folder = '';
public function __construct() {
$this->host = $_SERVER['HTTP_HOST'];
$this->folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
}
public function home() { /*function to redirect the user ot his profile page, or admin page if he is an administrator*/
if(admin_class::isadmin()){
header("Location: http://$host$folder/adminindex.php");
} else {
header("Location: http://$host$folder/profile.php");
}
exit();
}
public function welcome() {
header("Location: http://$host$folder/index.php");
exit;
}
public function loan() {
header("Location: http://$host$folder/loan.php");
exit;
}
}
Instead of calling simply redirect_to_home(); this would be invoked by redirect::home();.
Hope that helps.
try this
host = $_SERVER['HTTP_HOST'];
$folder = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
function redirect_to_home(){
global $host, $folder;
if(admin_class::isadmin()){
header("Location:http://$host$folder//adminindex.php");
exit();
}
else{
header("Location:http://$host$folder/profile.php");
exit();
}
}
If this works then change this in all other functions
GOOD LUCK!!
DOCUMENTATION http://php.net/manual/en/language.variables.scope.php
On a page on I'm working on, it can be password protected, so a user would have to login before they can see the content. After they login the following code is called:
//In one file
function current_page_url() {
return 'http://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
}
//In Another file
generate_page_cookie();
header('Location: ', current_page_url());
exit;
Which doesn't work. The following however does:
generate_page_cookie();
header('Location: ', 'http://example.com');
exit;
It seems strange that using a string in the header function works, but a function call within the header function doesn't.
I think it might have something to do with accessing the $_SERVER variable, but I'm not sure.
Change your , to ., php use . to concat strings.
header('Location: ' . current_page_url());
It's possible this question exists elsewhere, and if so, I apologize. After searching for an hour without success, I can't help but think I'm on the wrong track.
Essentially what I am looking for is a method to enforce a description or title in a page's URL. I am using CodeIgniter, so it is pretty simple to make a pretty URL go where ever I wish.
I could have:
http://mysite.com/controller/function/what-ever-i-want/can-go-here
and it will always go to:
http://mysite.com/controller/function/
with the variable values what-ever-i-want and can-go-here
What I would like is for the URL to be automatically rewritten to include the title if only the controller/function is given.
So, if someone went to:
http://mysite.com/controller/function/
it should automatically rewrite the url as
http://mysite.com/controller/function/a-more-descriptive-title/
A great example of the functionality that I am talking about is the SO URL. if you go to https://stackoverflow.com/questions/789439 it will automatically rewrite it to https://stackoverflow.com/questions/789439/how-can-i-parse-descriptive-text-to-a-datetime-object
I suspect that mod_rewrite is involved, but I would like to come up with the solution that works most gracefully with CodeIgniter.
I am very new to the pretty-url scene and desperately call upon the advice of someone with more experience. Thanks in advance for any help given!
I used Fiddler2 to see how Stackoverflow does this.
Part of the respnse from http://stackoverflow.com/questions/12205510/
HTTP/1.1 301 Moved Permanently
Location: /questions/12205510/how-can-i-enforce-a-descriptive-url-with-codeigniter
Vary: *
Content-Length: 0
So basically when we go to controller/function/ we need to redirect user to controller/function/my-awesome-title. I've written simple controller that does just that:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Controller extends CI_Controller
{
protected $_remap_names = array();
function __construct()
{
parent::__construct();
$this->_remap_names['func'] = "My Awesome Title";
}
function _remap($method, $arguments)
{
if(
isset($this->_remap_names[$method])
&& sizeof($arguments) == 0
&& method_exists($this, $method)
)
{
$this->load->helper("url");
$title = str_replace(" ", "-", $this->_remap_names[$method]);
$title = strtolower($title);
$url = strtolower(__CLASS__)."/$method/$title";
$url = site_url($url);
// if you dont want to have index.php in url
$url = preg_replace("/index\.php\//", "", $url);
header("HTTP/1.1 301 Moved Permanently");
header("Location: $url");
header("Vary: *");
}
else
{
call_user_func_array(array($this,$method), $arguments);
}
}
function func()
{
echo "<h1>";
echo $this->_remap_names[__FUNCTION__];
echo "</h1>";
}
};
Docs for CodeIgniters _remap function can be found here in Remapping Function Calls section.
I don't use CodeIgniter, but you should be able to place code in a controller's __construct, or in some kind of pre-action event if CI has those.
You just look up the appropriate URL for the entity being viewed and do a 301 redirect if necessary.
I am developing a personal framework purely on PHP.
Let's say I am in a method inside a controller and I want to redirect to another page how would I do that? At least conceptually.
If your developing MVC You should have an input class and an output class (I/O), you should create a function called redirect within the output class and build the new url from your base url like so:
public function redirect($controller,$method = "index",$args = array())
{
global $core; /* Guess Obviously */
$location = $core->config->base_url . "/" . $controller . "/" . $method . "/" . implode("/",$args);
/*
* Use #header to redirect the page:
*/
header("Location: " . $location);
exit;
}
This way within your controller you can simply use the input class do your redirect for you.
class MyController extends BaseController
{
public function login()
{
if($this->library->session->exists("user_logged_in") === false)
{
$this->library->output->redirect("MyController","login",array("from:login"));
}
}
/*
..More Here
*/
}
header("Location: http://domain.com/folder/page.html", 301);
exit();
This code must be the first output of the script. You can not perform redirection after generating any output to the client. Once you have sent the redirection to the client, you can exit the script because any additional output generated would not be seen by the user.