Good evening guys, I have a problem with the attribute href because I don't know how to access folders, I give an example:
I stay in root/store/products/details.php
I want to go root/index.php
My answer is : How do I arrive to root/index.php ? I has been trying with ../../index but that didn't worked
If root is the base path on your domain, e.g. example.com/root, then if you're at example.com/root/store/products/details.php, you would have a link like below to get to your root. This href will actually work on any page. If you use ../../index.php it will only work on some pages.
Root
This is how I make all of my template links portable.
For files processed Server-Side you would link by using the SERVER_ROOT and for links in an -a href=...' you would use the SITE_ROOT.
define('SERVER_ROOT', dirname( __FILE__ ) . DS);
$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off' ?
'https://' : 'http://') . $_SERVER['SERVER_NAME'];
if (strlen( $url ) <= 10) $url = null; // For IDE Support
define( 'SITE_ROOT', $url . DS); // The base URL
So to implement the above
Post Score</li>
Use <a href="../../index.php">
Demo Snippet:
var base = 'https://example.com/store/products/details.php'
var relative = '../../index.php'
var target = new URL(relative, base).toString()
console.log(target)
PHP defaults to index.php so you can link to
Link text
By prefixing a URL with /, like "/root/" instead of "root/", you are forcing it to read from the root of your webhost or with a VPS the directory of the website.
If you customised the DirectoryIndex you can do Link
Related
I have a php script that contains login and logout links that i want to use in my whole website.
The intention is to have a variable that contains the URL that can be used through the entire site and never changes with the current page location
Using $_SERVER['SERVER_NAME'] . dirname($_SERVER['REQUEST_URI']) i get:
localhost/~MyName/MySite/current_folder
and it changes depending on the current location
Using __DIR__ i get:
/Users/MyName/Sites/MySite/php
Where "php" is the folder that contains the script
What i'm searching is:
localhost/~MyName/MySite/php
Where i'm in local, and:
https://www mysite com/php
Where i'm on the internet
I want the path to be stored in a variable and used in the script:
<?php
echo ("<a href= '$path . /logout.php'> Logout </a>");
?>
place in your webroot index.php file at the top
define('URL', filter_var(rtrim('http'.(((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') || $_SERVER['SERVER_PORT'] == 443) ? 's' : '').'://'.$_SERVER['SERVER_NAME'].str_replace('\\', '/', dirname($_SERVER['PHP_SELF'])), '/').'/', FILTER_SANITIZE_URL));
in an MVC setup, this will allow URL to always equal your web root.
I'm currently working on a website that's hosted on our development server, I want to make some links that won't break when I move the site onto the new domain, so I plan to use to first get the root url, but how can I use that to make a link to the page at www.domain.com/blog?
Thanks a lot, hope that makes sense, couldn't really find a better way to word it!
Thanks,
Ethan
You can either use relative paths or $_SERVER['HTTP_HOST'];
For example :
$baseUrl = "http://" . $_SERVER['HTTP_HOST'];
$myLink = $baseUrl."/mypage";
Define in a common script like db.php / config.php
$application = 'local';
//$application = 'web';
if($application == 'local'){
define("SITEROOT","localhost/domain.com/");
$websitelink = "http://".constant("SITEROOT");
}
if($application == 'web'){
define("SITEROOT","www.domain.com/");
$websitelink = "http://".constant("SITEROOT");
}
Now in your menubar or where you want to add link to any page..e.g. for blog..
Go To Blog
Go To Forum
About Us
When you go online , simply disable $application = 'local' and comment out $application='web' like below
//$application = 'local';
$application = 'web';
With this approach, you can define many parameters and can use them anywhere in website....Just changing $application does the trick...
I have received website code from somebody else & it's giving a lot of errors in loading. Every CSS, image or anything is loaded by this code
href="<?php echo asset_url();?>/css/...
Now I checked, asset_helper.php & found out this
function asset_url(){
return BASE_URL.'public'
}
In my config.php, line says $config['base_url'] = '';.
Finally when I tried to echo asset_url();, it gives me https://somerandomwebsite.com/.... I'm not sure from where this is coming from.
Sorry I'm new to CodeIgnitor & tried everything I could to find out but there was no luck. Can anybody help me in this?
Your Helper function might be using constant function. Check your constan.php file.
define(BASE_URL, "http://test.com");
First things first: the manual!
https://codeigniter.com/user_guide/helpers/url_helper.html?highlight=base_url#base_url
ok so they made a new function just to deal with an extra subfolder name in stead of just doing base_url(). 'public' or base_url('public') and have defined a BASE_URL somewhere which is not part of the core of CI. Track that BASE_URL down and kill it if possible. You can and should use base_url().
I use this for base_url (avoids hardcoded url):
$protocol = ( isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on' ) ? 'https://' : 'http://';
$config['base_url'] = $protocol . $_SERVER['HTTP_HOST'] . str_replace(basename($_SERVER['SCRIPT_NAME']), "", $_SERVER['SCRIPT_NAME']);
I usually autoload the thing in config/autoload.php:
$autoload['helper'] = array('url');
I config the multi-language setting dynamically using the locale filter. Which fetch the sub-domain name to determine the language.
function load_custom_language($locale) {
// get the locale code according to the sub-domain name.
// en.mysite.com => return `en`
// zh.mysite.com => return `zh_CN`
// tw.mysite.com => return `zh_TW`
// etc..
}
add_filter('locale', 'load_custom_language');
That works for the index page, but when I redirect to another page, because of the settings of home and siteurl, it always redirects my site to the original one (www.mysite.com).
So I'm curious to find a dynamic way to filter the home and siteurl according to the request, because I might use more than one sub-domain for mysite and I have only one settings for the two settings.
You can override the admin settings in the wp-config.php file.
So if you want something dynamic, the following should work:
//presumes server is set up to deliver over https
define('WP_SITEURL', 'https://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', 'https://' . $_SERVER['HTTP_HOST']);
This needs to added before the line
require_once(ABSPATH . 'wp-settings.php');
or else you may have problems with some content using the wrong URLs, especially theme files.
I've found another pretty way to achieve the work:
After I checked for the source code of the kernel, I found that there are distinct filters called option_xxx on each options.
So, for my task, I tried to use the option_siteurl and option_home filter to hold that options to load, just to prevent the option to load, maintaining the SERVER_NAME it has:
function replace_siteurl($val) {
return 'http://'.$_SERVER['HTTP_HOST'];
}
add_filter('option_siteurl', 'replace_siteurl');
add_filter('option_home', 'replace_siteurl');
Using this way, it has no need to change the wp_config.php file, and can be easily add to a theme or a plugin.
To set dynamically the domain and as well as the protocol (http or https), use:
// Identify the relevant protocol for the current request
$protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https" : "http";
// Set SITEURL and HOME using a dynamic protocol.
define('WP_SITEURL', $protocol . '://' . $_SERVER['HTTP_HOST']);
define('WP_HOME', $protocol . '://' . $_SERVER['HTTP_HOST']);
You can get a base URL with $_SERVER["SERVER_NAME"], but sometimes if you have different projects in the same website, you need to save them in different directories. For example, if you have /var/www/myproject/, the real base URL is http://yourhost.com/myproject/.
If you try with $_SERVER["SERVER_NAME"] + $_SERVER["REQUEST_URI"], it gives the full request URL, and the problem becomes worse when you use friendly URLs. For example, if you use it in something like http://yourhost.com/myproject/controller/action/ you can't get the real base URL (yourhost.com/myproject).
How can I get the real base URL in these situations?
If I understood you right, you may need this..
$uri = isset($_SERVER['REQUEST_URI']) ? strip_tags($_SERVER['REQUEST_URI']) : '';
$urlvariables = (substr($uri, 0, 1) == '/') ? substr($uri, 1) : $uri;
$variables = explode('/', $uri);
echo $whatyouneed = $_SERVER['HTTP_HOST'] . '/' . $variables['1'];
You may give it a try.
Define it. If you have any sort of global project configuration at all (you should), you can set it up there. That’s more or less how CodeIgniter (for example) does it.
define('BASE_URL', '/myproject/'); # Include the host name if you truly must
To calculate it, put this code in your project root:
define('BASE_PATH', str_replace('\\', '/', __DIR__));
define('BASE_URL', preg_replace('|/+|', '/', strtolower(preg_replace('|^'.str_replace('\\', '/', realpath($_SERVER['DOCUMENT_ROOT'])).'|i', '', BASE_PATH))));