so I'm editing a PHP site (I'm a Python guy). What I need is a way to maybe redirect such that when someone navigates to the site e.g. www.mysite.com, the homepage should be served. The system used to serve other pages is as sush: to navigate let's say to the contacts page, we use www.mysite.com?page_id=contact-us. The query string helps the server side code to know what page to serve. So what I want is that when a user navigates to the site by typing www.mysite.com, he should get to the page www.mysite.com?page_id=home.
Thank you.
Sample code:
$page = isset($_GET['page_id']) ? $_GET['page_id'] : null;
if ($page !== null) {
//redirect to correct page
require("modules/inside.php");
} else {
//redirect to 'home'
header('Location: https://www.ndovucard.com?page_id=home');
require("modules/home.php");
}
Try...
if (!isset($_GET['page_id'])){
header('Location: www.mysite.com?page_id=home');
}
Something like this?
$page = (isset($_GET['page_id'])) ? $_GET['page_id'] : 'home';
echo $page; // 'home' or provided page_id
From what you've said it sounds like you need a:
if (!isset($_GET['page_id'])) $_GET['page_id'] = 'home';
Personally I prefer a full URL rewrite system rather than passed by URL query as it's believed search engines prefer contact-us.html rather than page_id=contact-us
Edit:
You could alternatively do:
if (!isset($_GET['page_id'])) {
header('Location: /?page_id=home');
die(); // stop any further processing
}
something like:
$page = isset($_GET['page_id']) ? $_GET['page_id'] : 'home';
/* check that page is a valid page else serve error page */
/* then redirect to the correct $page */
header('Location: www.mysite.com?page_id=' . $page);
exit();
By the way, you need some way to check that $_GET['page_id'] is always a valid page_id before redirecting.
Related
I have a login page that submits to another page while adding a string to the end of the url. Would look something like this 'http://example.com?klc' I know I can use $_SERVER["QUERY_STRING"] to get the string, but now I need to use it in a function to direct the user to a different page, based on the string. This is what I have written in the target file
<?php
$access = $_SERVER["QUERY_STRING"];
function user_admin_redirect($access){
if ($access = "ppl"){
redirect_to("ppl_admin.html");}
else ($access = "klc"){
redirect_to("klc_admin.html");}
}
}
user_admin_redirect($access);
but for some reason the script dies. Any tips would be welcomed. Also, I have the system setup on my website, contact me if you are willing to help I can give you a test login.
$access = $_SERVER["QUERY_STRING"];
function user_admin_redirect($access){
if ($access == "ppl"){
redirect_to("ppl_admin.html");}
else if($access == "klc"){
redirect_to("klc_admin.html");}
}
}
user_admin_redirect($access);
You need to use == and not = when using if
You need to use else if and not just else
I am assuming redirect_to is some custom function that you have written which will redirect you to the mentioned page. If not, you should use header('Location: ' . $location);
I don't think redirect_to is a built-in PHP function.
You might need to use
header("Location:".$myPhpScript);
or you could define redirect_to, like:
function redirect_to($location){
header("Location:".$location);
}
See more here:
php redirect_to() function undefined
How to make a redirect in PHP?
I have a problem with the Mobile Detection Script.
There are two scenarios:
First the script should detect if it's a mobile or not. If mobile, than redirect to another page (this works fine).
The second query should determine, if the person is on the root page or not. If it's not the root page, the layout should be the classic one. (no redirection)
But when I add this line there won't be anymore redirection, even if I open the root page on a mobile.
I also tried to destroy the session on the google_mobile.php (redirected page) and set the $_SESSION['layoutType'] = 'mobile', but anyway the session is set to classic when I open the root page.
Thanks for your help!
Here is the script:
session_start();
require_once 'Mobile_Detect.php';
function layoutTypes() {
return array('classic', 'mobile');
}
function initLayoutType() {
// Safety check.
if (!class_exists('Mobile_Detect'))
return 'classic';
$detect = new Mobile_Detect;
$isMobile = $detect->isMobile();
$layoutTypes = layoutTypes();
// Set the layout type.
if (isset($_GET['layoutType'])) {
$layoutType = $_GET['layoutType'];
} else {
if (empty($_SESSION['layoutType'])) {
$layoutType = ($isMobile ? 'mobile' : 'classic');
} else {
$layoutType = $_SESSION['layoutType'];
}
//check if it's the root page
if ($_SERVER['REQUEST_URI'] != "/")
$layoutType = 'classic';
}
// Fallback. If everything fails choose classic layout.
if (!in_array($layoutType, $layoutTypes))
$layoutType = 'classic';
// Store the layout type for future use.
$_SESSION['layoutType'] = $layoutType;
return $layoutType;
}
$layoutType = initLayoutType();
if ($_SESSION['layoutType'] == 'mobile') {
header("Location: www.example.com/google_mobile.php");
exit;
}
I've tested your code, it seems to work as you described. I'd guess it is a session issue.
session_destroy() does not clear your previous session state in the immediate session. That means your $_SESSION would still be "dirty" in a script even if session_destroy() is the first line in it. It's safer to clear cookies from your browser instead.
One other possible problem would be query string. You're checking the REQUEST_URI and it includes any query string on URI. "/?foo=bar" is certainly not "/". You may want to check SCRIPT_NAME (i.e. $_SERVER['SCRIPT_NAME'] == 'index.php) instead.
In messaging system of my project when you get a message from a user you a email alert saying that the another user has sent a message to view the message click here (i.e the url of message) So if the user is not logged in to system he gets redirect to login page and after login it should get back to the referer url. I have made a basecontoller in core folder and extending the CI_controller the authenticating code is as follows.
function authenticate($type = 'user')
{
if($type == 'user')
{
if($this->user_id)
{
// user is logged in. check for permissions now
}
else
{
// user isnt logged in. store the referrer URL in a var.
if(isset($_SERVER['HTTP_REFERER']))
{
$redirect_to = str_replace(base_url(),'',$_SERVER['HTTP_REFERER']);
}
else
{
$redirect_to = $this->uri->uri_string();
}
redirect('user/login?redirect='.$redirect_to);
exit;
}
}
if($type == 'admin')
{
if($this->session->userdata('admin_id') && $this->session->userdata('user_type') ==5)
{
// Admin is logged in
}
else
{
redirect('admin/login');
exit;
}
}
}
The referer url is "http://example.com/project/pm/view_conversation?id=11"
now the problem is I am getting referer url till view_conversation and not able to get the id part.
Any suggestion ?
Thank you.
This can help:
CI 2+
https://www.codeigniter.com/userguide2/libraries/user_agent.html
CI 3+
http://www.codeigniter.com/userguide3/libraries/user_agent.html
Below solution is for Codeigniter version 3
$this->load->library('user_agent');
if ($this->agent->is_referral())
{
echo $this->agent->referrer();
}
UPDATE: interesting and useful information on how to obtain referrer information with the same user_agent library
https://www.tutorialandexample.com/user-agent-class/
How about just
redirect($_SERVER['HTTP_REFERER']);
Using php's $_SERVER global variable.
This worked for me!
Put that code in your Login Controler
function index() {
$this->load->library('user_agent'); // load user agent library
//Set session for the referrer url
$this->session->set_userdata('referrer_url', $this->agent->referrer() );
}
After Login Redirection Code
// user is authenticated if referrer is there
if( $this->session->userdata('referrer_url') ) {
//Store in a variable so that can unset the session
$redirect_back = $this->session->userdata('referrer_url');
$this->session->unset_userdata('referrer_url');
redirect( $redirect_back );
}
Because you have double question mark in the url, the browser ignores the url part after the second one. Use urlencode for you redirect part, like so:
redirect('user/login?redirect='.urlencode($redirect_to));
I've tested it out and it works this way.
By default CI is configured to ignore the query part of the URL (the part after the '?').
See: http://codeigniter.com/user_guide/general/urls.html
I need some javascript or php code to detect if user has linked directly to me site's page so I can then redirect it to the homepage.
Is this possible?
Check is the the host of your server is in the http_referer (it is the last url visited by the user).
function is_direct_link() {
return (!empty($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], $_SERVER['HTTP_HOST']) === false);
}
if (is_direct_link()) {
header('location: http://www.google.com');
exit();
}
<?php
$referrer = $_SERVER['HTTP_REFERER'];
// see if they are visiting from another website
if (!empty($referrer) && stripos($referrer, 'mydomain.com') === false)
{
// redirect them to the main site
header('Location: http://mydomain.com/');
// stop php from going further
exit;
}
Something like that is (i think) what you're looking for.
If the referrer is empty, this will redirect to the home page:
<?php
if(!empty($_SERVER['HTTP_REFERER']) && stripos($_SERVER['HTTP_REFERER'], 'example.com') header("Location: example.com");
?>
Note that the referrer is not bulletproof by any stretch but it does the basic job.
How would I check if the user did not come from the same website? So for example if a user types in the URL in the browser to a direct page then they would have accessed that page without coming from another page in the site.
So something like:
if(!user not from same url)
{
//do some stuff
}
$this->referer() in controller?
Edit: Well, for your purpose, you can directly use this:
$referer = env('HTTP_REFERER');
if(empty($referer)){ echo 'jazz'; }
because $this->referer() behaves a little differently. It won't return false or null or empty string.
$referer = env('HTTP_REFERER');
if(empty($referer) && $this->Session->check('Auth.redirect') == false)
{
echo 'user didn\'t come website and wasn\'t redirect from the auth component';
}