in my application i'm currently using "pretty urls" with apache rewrite. So:
article.php?id=123&title=hello-bob
becomes:
/articles/hello-bob.123
In my local dev environment, I'm not using pretty urls and I ended up writing a function to pass links through, to get the correct one based on a setting:
public function get_link($id, $title, $additional = NULL)
{
$link = '';
$nice_title = core::nice_title($title); // remove spaces, special characters and so on so-it-looks-like-this
if ($this->config('pretty_urls') == 1)
{
$link = 'articles/'.$nice_title.'.'.$id;
if ($additional != NULL)
{
$link = $link . '/' . $additional;
}
}
else
{
$link = 'index.php?module=articles_full&aid='.$id.'&title='.$nice_title;
if ($additional != NULL)
{
$link = $link . '&' . $additional;
}
}
return $this->config('website_url') . $link;
}
Is this really wasteful? I'm wondering how other applications and sites manage this, when they allow people to enable pretty urls and show the correct links in the rendered content?
Related
I'm in search of a foolproof method via code to tell if an ecommerce platform id running on woocommerce or shopify.
ATM, I came up with the code below that checks if there's a URL with "wp-admin" or "admin", but its not foolproof.
$url1 = "https://www." . $_POST['domain'] . "/wp-admin";
$url2 = "https://www." . $_POST['domain'] . "/admin";
// Use get_header() function
$headers1 = #get_headers($url1);
$headers2 = #get_headers($url2);
// Use conditio to check existence of URL
if($headers1 && strpos($headers1[0], '200'))
{
$eCommercePlatform = "WordPress/Woocommerce";
}
elseif($headers2 && strpos($headers2[0], '200'))
{
$eCommercePlatform = "Shopify";
}
else
{
$eCommercePlatform = "Not Shopify or Woocommerce";
}
Any other good hacks/suggestions?
I would use something like that:
// Mimic the browser
ini_set('user_agent', 'Mozilla/5.0');
$headers = #get_headers($url, 1);
if (isset($headers['X-Shopify-Stage'])) {
$eCommercePlatform = "Shopify";
} elseif (!empty($headers['X-Powered-By']) && $headers['X-Powered-By'] == 'WP Engine') {
$eCommercePlatform = "WordPress/Woocommerce";
} else {
$eCommercePlatform = "Not Shopify or Woocommerce";
}
Explanation:
Checking the /wp-admin path seems is not the best way to detect WordPress as on the first WP website I tested - it returned 403 Forbidden. I assume others may do the same for security purposes.
I found that all Shopify sites return X-Shopify-Stage header (at least for the time being) which can be used.
Ok my website incorporates different languages, I did:
$lang = isset($_GET['lang']) ? $_GET['lang'] : "";
if (!empty($lang)) {
$curr_lang = $_SESSION['curr_lang'] = $lang;
} else if (isset($_SESSION['curr_lang'])) {
$curr_lang = $_SESSION['curr_lang'];
} else {
$curr_lang = "he";
}
if (!empty($pb20_14users->language == he)) {
echo "&lang=he";
}
if (file_exists("languages/" . $curr_lang . ".php")) {
include "languages/" . $curr_lang . ".php";
} else {
include "languages/he.php";
}
// Returns language key
function lang_key($key)
{
global $arrLang;
$output = "";
if (isset($arrLang[$key])) {
$output = $arrLang[$key];
} else {
$output = str_replace("_", " ", $key);
}
return $output;
}
The text has changed and everything is perfect, but I want to change my URL
&lang=he
But don't go through all the existing pages manually, they were built like this:
<div class="links">
Home<br />
Register<br />
Statistics<br />
Forgot password?
</div>
How do I make all addresses end & lang = he without going through one by one, if it matters I made a table in SQL called language and there is the word he, maybe it will help the thinkers, thank you very much
I recommend you go a (basic) nice url system, your urls will become /en/?page=foo which isnt the biggest rewrite in the url.
RewriteRule ^([a-z]{2})/(.*) /$2&lang=$1 [L]
I havent tested that line, but it should be close. We take everything before the slash (en) and add that to the rest (?page=foo) which results in ?page=foo&lang=en for your script.
Now you can do this and use CURRENT_LANG everywhere in your PHP for the language.
$lang = in_array($_GET['lang'], ['en', 'nl']) ? $_GET['lang'] : 'en';
define('CURRENT_LANG', lang);
Or try to use the <base /> tag in html.
I am trying to translate these pages from English to French:
/about.php
/contact.php
/paintings.php?id={$id}&title={$title}
I created the following link:
<?php if($language !== 1): ?>English<?php else: ?>French<?php endif; ?>
With this code behind it:
$url = $_SERVER["REQUEST_URI"];
if (isset($_GET['lang'])) {
$pattern1 = '&lang=' . $_GET['lang'];
$pattern2 = '?lang=' . $_GET['lang'];
$patternTotal = array($pattern1, $pattern2);
$url = str_replace($patternTotal, '', $url);
$language = 1;
}
else if (strpos($_SERVER["REQUEST_URI"], '?')) {
$url .= '&lang=en';
$language = 0;
}
else {
$url .= '?lang=en';
$language = 0;
}
What I want for it to do is to return 1 to $language if the lang parameter is set to any value. Then I will use an if statement to fetch and display the content accordingly.
However I realized that this url /paintings.php?id={$id}&title={$title} looks bad and I used mod_rewrite to get it to this version /{$id}/{$title} with this RewriteRule ^([^/]*)/([^/]*)$ /photo.php?id=$1&title=$2 [L]. And things broke. While the $url works on all the other pages on the paintings one it doesn't.
I have a feeling that I got it all wrong. How can I make it work? Also, is there a more efficient way in doing this?
I have the following problem: I have a set of domains with the same url structure:
domain-a.com/london/
domain-b/london/
domain-c/london/
I want to do the following thing:
If you are on domain-a.com/london/, I want "related" links underneath pointing to domain-b.com/london/ and domain-c.com/london/
I want these links to appear automatically using the URL of the current page, remove the domain so that only the rest is left - in my example: /london/ and add the other domains in front of this.
I know I have to use echo $_SERVER['REQUEST_URI']; to get the rest of the URL but I don't know how to create a link using this function.
<?php
$url = $_SERVER['HTTP_HOST'];
$uri = $_SERVER['REQUEST_URI'];
function generateLink($url, $uri){
if(strpos($url,'domain-a.com') !== false){
$link = 'http://domain-b.com' . $uri;
return $link;
}else if(strpos($url,'domain-b.com') !== false){
$link = 'http://domain-c.com' . $uri;
return $link;
}else if(strpos($url,'domain-c.com') !== false){
$link = 'http://domain-a.com' . $uri;
return $link;
}
}
?>
Link
I'm trying to force different modes of debugging based on different development urls using PHP. I currently have this set up:
$protocol = strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https') === FALSE ? 'http' : 'https';
$host = $_SERVER['HTTP_HOST'];
$req_uri = $_SERVER['REQUEST_URI'];
$currentUrl = $protocol . '://' . $host . $req_uri;
$hostArray = array("localhost", "host.integration", "10.105.0"); //Don't use minification on these urls
for ($i = 0; $i < count($hostArray); $i++) {
if (strpos($currentUrl, $hostArray[$i])) {
$useMin = false;
}
}
However, using this method, you would be able to trigger the $useMin = false condition if you were to pass any of the strings in the host array as a parameter, for example:
http://domain.com?localhost
How do I write something that will prevent $useMin = false unless the URL starts with that condition (or is not contained anywhere after the ? in the url parameter)?
Don't use the $currentUrl when checking $hostArray, just check to see if the $host itself is in the $hostArray.
If you want to check for an exact match:
if(in_array($host, $hostArray)) {
$useMin = false;
}
Or maybe you want to do this, and check to see if an item in $hostArray exists anywhere within your $host:
foreach($hostArray AS $checkHost) {
if(strstr($host, $checkHost)) {
$useMin = false;
}
}
And if you only want to find a match if $host starts with an item in $hostArray:
foreach($hostArray AS $checkHost) {
if(strpos($host, $checkHost) === 0) {
$useMin = false;
}
}
I can't comment so I'll post here. Why do you check the host array with the url, why not check it directly with the host as in:
if (strpos($host, $hostArray[$i])) {