Codeigniter routing segments/params - php

$route['segment1/(:any)/(:any)'] = "myController/$1/$2";
in this case, I will have to link segment1/someMethod/someParam, but what if I don't have params in the url, i will need to write this:
$route['segment1/(:any)'] = "myController/$1";
$route['segment1/(:any)/(:any)'] = "myController/$1/$2";
thus the both cases will now work, so the question is: Can I write those 2 lines of code in one shot?

this:
$route['segment1/(:any)'] = "myController/$1";
$route['segment1/(:any)/(:any)'] = "myController/$1/$2";
is duplicating,they do the same thing, use just one of them, i suggest
$route['segment1/(:any)'] = "myController/$1";
hope to be clear, when using this "myController/$1" you are saying everything following myController/ should be routed, and it works also if no $1 params exists.
Definitely use just one of them and don't scare about not having params, it works like a charm ;)

Related

Fortnite API html

I want to put my fortnite stats on my website HTML/CSS. I find this.
But I don't know php very good, so I need your help. Must I delete this: 'your_api_key' and put : your_api_key without the ' ' ?
And lines like this:
$this->auth = new Fortnite_Auth($this);
$this->challenges = new Fortnite_Challenges($this);
$this->leaderboard = new Fortnite_Leaderboard($this);
$this->items = new Fortnite_Items($this);
$this->news = new Fortnite_News($this);
$this->patchnotes = new Fortnite_PatchNotes($this);
$this->pve = new Fortnite_PVE($this);
$this->status = new Fortnite_Status($this);
$this->weapons = new Fortnite_Weapons($this);
$this->user = new Fortnite_User($this)
Must I modify something?
(here are some informations:
-user_id: 501b9f2dfda34249a2749467513172bf
-username: NoMaD_DEEPonion
-platform: pc
-windows: season 5
)
For all this code, I used xammp server (I hope it's good)
Thank you for your help
You should always quote '' your key/strings. So it would look like:
$api->setKey('123qwerty456');
$api->user->id('NoMaD_DEEPonion');
Please read their documentation. You're supposed to get an API key from them to get started. And you don't have to edit Client.php. You're supposed to include the files instead. Try including Autoloader.php, since it requires the necessary files.
XAMPP is alright for development, but not suitable for production/public. Good luck!
What #sykez wrote.
I personally use https://fortniteapi.com/ to get the data. They also have a sweet POSTMAN page with different requests https://documenter.getpostman.com/view/4499368/RWEjoGqD
At last. I am not really sure that this will be a good project to start learning PHP from. I really suggest that you get to understand the basics of PHP first before you jump into API calls, processing arrays and more.

CakePHP beforeFilter find

I love and hate Cakephp at the same time.
I need the current domain name and id in virtually every controller in a multi tenant site.
So, in the AppContoller beforeFilter...if I do this...
$dname = $_SERVER['HTTP_HOST'];
$this->set('domain',$dname); //only to show test output in the view.
I have access to $domain in the view without any problem.
Good so far. I know I have access to SERVER vars.
Then, my find function works fine when I hard code the domain name like this...
$this->loadModel('Domain');
$domainName = $this->Domain->find('first',array('conditions' => array('Domain.name' =>'test.localhost.com')));
However, when I try to use $dname within the condition like this, it fails.
$this->loadModel('Domain');
$dname = $_SERVER['HTTP_HOST'];
$domainName = $this->Domain->find('first',array('conditions' => array('Domain.name' => $dname)));
I'm feeling like I'm close, but... wth?
Oh my gosh. I am so embarrassed. It works fine. I didn't have an entry in the db for the domain I was using, so it couldn't find anything. Sorry.
Sometimes that whole forest/trees thing.... well you know.
You can pr($dname); within the before filter and it will show on every page. also you may want to try using strtolower() to make sure that no other letters are capitalized when putting it in the condition.

How to find and replace a line in multiple files with PHP

I want to create a installer for my current project, that automatically modifies a dozens of config files.
So if the form was sent, the PHP script should look in which config file the searched option is and change it. Before you ask, I cant put the files together ;) .
A basic config line looks like this:
$config['base_url'] = 'test';.
I tried to use str_replace()but this didn't work because I don't know what is currently in the variable.
So I need a function that searches for $config['base_url'] = '%'; in multiple files and replaces it with $config['base_url'] = 'new_value'; (for example).
I realise the answer's accepted, and originally I deleted this, however, in the comments you mention the config being editable, which presumably means by other users, so you can't guarantee the spacing will match, nor that they'll use ' instead of " always, so the following is perhaps a little more forgiving
$name = 'base_url';
$value = 'new_value';
$config = '$config["base_url"] = "old_value";';
$config = preg_replace('/\[(?:\'|\")'.$name.'(?:\'|\")\]\s*=\s*(\'|\")(.*)\\1;/', "['".$name."'] = '$value';", $config);
echo '<pre>', var_dump($config), '</pre>';
You can use a regular expression like the following:
/\$config\['base_url'\] = '[a-zA-Z0-9]';/
Which you would have to adapt to each line.
A better solution, in my opinion, would be to create a template config file with lines like the following:
$config['base_url'] = '%BASE_URL%';
Where you could simply use str_replace().

Reduce link (URL) size

Is it possible to reduce the size of a link (in text form) by PHP or JS?
E.g. I might have links like these:
http://www.example.com/index.html <- Redirects to the root
http://www.example.com/folder1/page.html?start=true <- Redirects to page.html
http://www.example.com/folder1/page.html?start=false <- Redirects to page.html?start=false
The purpose is to find out, if the link can be shortened and still point to the same location. In these examples the first two links can be reduces, because the first points to the root, and the second has parameters that can be omitted.
The third link is then the case, where the parameters can't be omitted, meaning that it can't be reduced further than to remove the http://.
So the above links would be reduced like this:
Before: http://www.example.com/index.html
After: www.example.com
Before: http://www.example.com/folder1/page.html?start=true
After: www.example.com/folder1/page.html
Before: http://www.example.com/folder1/page.html?start=false
After: www.example.com/folder1/page.html?start=false
Is this possible by PHP or JS?
Note:
www.example.com is not a domain I own or have access to besides through the URL. The links are potentially unknown, and I'm looking for something like an automatic link shortener that can work by getting the URL and nothing else.
Actually I was thinking of something like a linkchecker that could check if the link works before and after the automatic trim, and if it doesn't then the check will be done again at a less trimmed version of the link. But that seemed like overkill...
Since you want to do this automatically, and you don't know how the parameters change the behaviour, you will have to do this by trial and error: Try to remove parts from an URL, and see if the server responds with a different page.
In the simplest case this could work somehow like this:
<?php
$originalUrl = "http://stackoverflow.com/questions/14135342/reduce-link-url-size";
$originalContent = file_get_contents($originalUrl);
$trimmedUrl = $originalUrl;
while($trimmedUrl) {
$trialUrl = dirname($trimmedUrl);
$trialContent = file_get_contents($trialUrl);
if ($trialContent == $originalContent) {
$trimmedUrl = $trialUrl;
} else {
break;
}
}
echo "Shortest equivalent URL: " . $trimmedUrl;
// output: Shortest equivalent URL: http://stackoverflow.com/questions/14135342
?>
For your usage scenario, your code would be a bit more complicated, as you would have to test for each parameter in turn to see if it is necessary. For a starting point, see the parse_url() and parse_str() functions.
A word of caution: this code is very slow, as it will perform lots of queries to every URL you want to shorten. Also, it will likely fail to shorten many URLs because the server might include stuff like timestamps in the response. This makes the problem very hard, and that's the reason why companies like google have many engineers that think about stuff like this :).
Yea, that's possible:
JS:
var url = 'http://www.example.com/folder1/page.html?start=true';
url = url.replace('http://','').replace('?start=true','').replace('/index.html','');
php:
$url = 'http://www.example.com/folder1/page.html?start=true';
$url = str_replace(array('http://', '?start=true', '/index.html'), "", $url);
(Each item in the array() will be replaced with "")
Here is a JS for you.
function trimURL(url, trimToRoot, trimParam){
var myRegexp = /(http:\/\/|https:\/\/)(.*)/g;
var match = myRegexp.exec(url);
url = match[2];
//alert(url); // www.google.com
if(trimParam===true){
url = url.split('?')[0];
}
if(trimToRoot === true){
url = url.split('/')[0];
}
return url
}
alert(trimURL('https://www.google.com/one/two.php?f=1'));
alert(trimURL('https://www.google.com/one/two.php?f=1', true));
alert(trimURL('https://www.google.com/one/two.php?f=1', false, true));
Fiddle: http://jsfiddle.net/5aRpQ/

How do you get a certain portion of the Document root with PHP?

I am working a website for my friends and myself, and it needs to be a bit secure.
I have an SQL database for storing user info,
I store the variables for the database outside the public files and have
a function to retrieve them.
With this function which is called getSQL_Info($n) it takes the
line in the text file and breaks it up and puts the
variables into an array and returns array[$n].
In order to load the file with the variables I must provide the location, instead of having the path written in the function, I would like to have it so you do something like
$_SERVER['DOCUMENT_ROOT'] and use that to get the base part of the path.
Essentially, I would like to have the path "home2/(mySecretUsername)/config/file.txt"
when it gives me "home2/(mySecretUsername)/public_html/fpi".
I kinda just need a good explanation of how to take the "home2/(mySecretUsername)/" part out and add "config/file.txt".
Thanks for your time,
if I explained my question poorly please do tell me and I will add further information.
-Michael Mitchell
Would this approach work for you?
function getConfigFile() {
$components = explode('/' , $_SERVER['DOCUMENT_ROOT']);
$componentLen = count($components);
if ($componentLen < 3) {
return ''; // Something goes wrong
}
$components2 = array_slice($path, 0, $componentLen - 2);
return implode( '/' , $components2) . '/config/file.txt'
}
It's obviously platform-dependent but looks like it does the job.

Categories