I am creating an API to feed some apps.
So the app could call these possible URLs to get information from the database;
mysite.com/api/v1/get/menus/list_tblname1.json.php
mysite.com/api/v1/get/menus/list_tblname1.json.php?type=arr
mysite.com/api/v1/get/menus/list_tblname2.json.php
mysite.com/api/v1/get/menus/list_tblname2.json.php?type=arr
In php I have already the code that grabs the tblname from the URL and give me back all the table content. It works good (it is not the final version).
But now I find myself copying and pasting the same code for each page where the URL points to. Here is the code:
<?php
header('Content-Type:application/json');
include_once '../../../../class/db.php';
$verb=$_SERVER['REQUEST_METHOD'];
$filePath=$_SERVER['SCRIPT_NAME'];
$split1 = explode("/", $filePath);
preg_match("/(?<=_)[^.]+/", $split1[5], $matches);
$tableName = $matches[0];
if ($verb=="GET") {
header("HTTP/1.1 200 ok");
if(isset($_GET['type']) && $_GET['type']=="arr"){
echo db::get_list($tableName,'arr');//Reply ARRAY
}
else{
echo db::get_list($tableName);//Reply JSON
}
}
else{
die("Nothing for you at this page!");
}
I mean, I have the same code inside each these pages.
list_tblname1.json.php
list_tblname2.json.php
I am not sure how to solve this situation but I think that this is case for
rewrite rules.
So, I think a possible solution is to create one page that could call
returncontent.php for example and create rules in the server that should point to the same page when certanlly pages are requested and pass the parameter $tableName to the page. I think I should pass the regex to my server and grab the $tableName with $_GET[] (I think) inside returncontent.php.
I am not sure about it.
I am using NginX.
How to implement it in this scenario?
As a rule, it's bad practice to parse a URI in NginX and pass the result downstream.
Rather: mysite.com/api/v1/get/menus/returncontent.php?file=list_tblname2.json
No changes to NginX needed. Parse the query param (file) in PHP.
Related
I'm pretty new to Yii, so this might be a silly question. I must be missing something somewhere. Plz help me out.
I have just written a simple code while I'm learning Yii.
I have a spark controller which has an action that looks like this:
public function actionDownload($name){
$filecontent=file_get_contents('images/spark/'.$name);
header("Content-Type: text/plain");
header("Content-disposition: attachment; filename=$name");
header("Pragma: no-cache");
echo $filecontent;
exit;
}
Now I have enabled SEO friendly URLs and echo statement in the view file to display the returned download file name.
But when I go to the URL,
SITE_NAME/index.php/spark/download/db5250efc9a9684ceaa25cacedef81cd.pdf
I get error 400 - your request is invalid.
Plz let me know if I am missing something here.
Yii keeps an eye on the params you pass into an action function. Any params you pass must match what's in $_GET.
So if you're passing $name then you need to make sure there's $_GET['name'] available.
So check your URL manager in /config/main.php, and make sure you're declaring a GET var:
'spark/download/<name:[a-z0-9]+>' => 'site/download', // Setting up $_GET['name']
Otherwise change your function to the correct param variable:
public function actionDownload($anotherName){
}
Did you create the URL correct? You should do it best with
Yii::app()->createUrl('/spark/download',array('name'=>$filename));
Make sure if you are properly supplying the parameter name ($name) in your request.
Use
SITE_NAME/index.php/spark/download/name/db5250efc9a9684ceaa25cacedef81cd.pdf
or
SITE_NAME/index.php/spark/download?name=db5250efc9a9684ceaa25cacedef81cd.pdf
instead of just SITE_NAME/index.php/spark/download/db5250efc9a9684ceaa25cacedef81cd.pdf
I am currently working on developing my first WHMCS Addon Module, and so far everything has gone very well. However, I need to make multiple content pages, and the only way to display output according to the Wiki Article is to echo it in the output function. How can I create individual pages when the only way to display content is via a single PHP function?
I am assuming using divs, and hiding the relevant divs, although not exactly the best method. It says you can use the "modulelink" variable to link back to the module, but I have no idea how to use this, or if it can be used for making multiple content pages.
http://docs.whmcs.com/Addon_Module_Developer_Docs
After some tinkering, it was much more simple than I realized, and the "modulelink" variable was just so you could link back to the page. To create additional pages, you can basically just do something along the lines of...
Datacenters
Then in the output function have...
$category = $_GET['catid'];
if ($category == "1" || $category == "")
{
//page 1 content here
}
else if ($category == "2")
{
//page 2 content here
}
and so on.
My pages contain this kind of url:
http://fashion.piliapp.com/author/http%3A%2F%2Fwww.wretch.cc%2Fblog%2Fwiwinnie/
which created by
"http://fashion.piliapp.com/author/" . rawurlencode("http://www.wretch.cc/blog/wiwinnie") . "/"
HTML looks like
<cite>♡winnie♡(。→v←。)♡</cite>
It occur lots of time on each pages, and I also added in sitemap.xml , but it only have one result in site:fashion.piliapp.com/author/
It should have thousands pages.
I think the problem are:
- Google considers as those pages are typo pages.
- CITE tag will not be indexed.
- this kind of urls are invalid.
Shell I change anything?
Thanks.
Yes, if you can change the url still, go ahead and do change it.
Just make sure everything still works fine if you change it :)
or if you want you, may use some base_encode, then base_decode techniques.
<?php
$url = base64_encode($url) . '.htm';
$url = "http://fashion.piliapp.com/author/" . $url;
?>
then when displaying the actual page, get url later by decoding it
<?php
$uri = $_SERVER['REQUEST_URI'];
$url = str_replace("http://fashion.piliapp.com/author/",'',$url);
$url = str_replace(".htm",'',$url);
$url = base64_decode($url);
//$url should contain the actual url afterwards
?>
I am creating a website in which user enters his position(which can only be the ones stored in my database). To make it user friendly, I want to show him the available choices of positions as he types the positions. Is there a way to cache all the values in SQL(around 30 to 40) in browser beforehand, so that user can see them while typing.
You can query your data base through PHP and most other server side scripts and call them through ajax. You can also use pure javascript with the new html5 specs (but they are only supported in webKit browsers at the moment).
If you want to do it without ajax, you can get the values when the user accesses the page and put them in a javascript array. This way when the computer reads the javascript the custom values are already there. The problem with this meathod is it will significantly slow your websites response time.
Example: (php/pesudocode)
<?php
$options = $mysql->getOptions(); // This is the pesudo-code bit.
echo 'options = [';
foreach ( $opt in $options )
{
echo '"' . $opt.name . '"' . ',';
}
echo '];"
?>
This gives something like
options = [ "opt1", "opt2", "opt3", ];
which should be read as an array in javascript. The only problem is the extra comma but you should be able to solve that easy enough. (and firefox didn't even give a warning)
I've seen a number of examples of the opposite, but I'm looking to go from an anchor/hash URL to a non-anchor URL, like so:
From: http://old.swfaddress-site.com/#/page/name
To: http://new.html-site.com/page/name
None of the examples at http://karoshiethos.com/2008/07/25/handling-urlencoded-swfaddress-links-with-mod_rewrite/ have functioned for me. It sounds like REQUEST_URI has the /#/stuff in it, but neither me nor my Apache (2.0.54) see it.
Any ideas, past experiences or successes?
Anything after the # is a fragment, and will not be sent to the webserver. You cannot capture it at any point there, you'll have to use a client-sided approach to capture those.
#RobRuchte : would it not be better to use window.location.hash, with a replace instead of a regular expression?
var redirectFragment = window.location.hash.replace(/^#/,'');
if ( '' !== redirectFragment ) {
window.location = 'http://new.html-site.com' + redirectFragment;
}
I'm the author of the post you linked to. Wrikken is correct, the content after the named anchor is not sent to the server unless something has mangled the URL along the way. On the client side, you need some JavaScript like this in your landing page to redirect the swfaddress links to corresponding URLs on another domain:
var re = new RegExp('#(.*)');
var redirectFragment = re.exec(document.location.toString());
if (redirectFragment!=null)
{
document.location = 'http://new.html-site.com'+redirectFragment[1];
}
I used a modified version of the answer by #m14t. This works for redirects that look like http://example.com/path/to/page#fragment --> http://example.com/path/to/page/fragment. Notice that I also concatenated the window.location.pathname for the redirect, otherwise I would not get the full path for the redirect. If the new file path is completely different from the old one, then this would not work.
var redirectFragment = window.location.hash.replace(/#/,'/');
if ( '' !== redirectFragment ) {
window.location = 'http://example.com' + window.location.pathname + redirectFragment;
}
In my case, I needed to build fragmented links into individual pages, which is part of what is commonly done to improve a website's SEO.