Using jquery and ajax to retrieve a set of image path - php

I am implementing an online book application. It is based on html.
Generally, the book is double page . However , only single page is shown for the first page and it is page 1. I need loading six page for each time. For instance, if user read the 4th 5th page , what I need to get are 2th 3th 4th 5th 6th 7th pages.
So my idea is provide the folder name and one page number (i , e.g. i =
5th) and return i-3,i-2,i-1 ,i , i+1, i+2 image paths.
Here is some sample code I find:
public function getImage(){
$params = $this->getRequest()->getParams();
//d($params);
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$src = $params['image_path'];
exit;
}
}
It is useful? and How can I convert it to accept the folder name/ page number? Thank you

Related

How to get last page url in twilio pagination php

$page = isset($input['page'])?$input['page']:0;
$perPageRecord = 10;
$calls = $this->twilio->calls->page(["to" => "+919876543210"],$perPageRecord,'',$page);
$data = [];
echo $calls->getNextPageUrl;
exit;
I am using above code to get next page url and it print successfully. But i want to print last page url while In php twilio.
Anyone can tell me how can i get last page url using twilio php.
Thanks
It looks like you will need to programmatically extract a returned range and manipulate the resulting data to get the X most recent results (last page).
Replacing Absolute Paging and Related Properties
Usage and Migration Guide for Twilio's PHP Helper Library 5.x

append to url in Laravel

I'm trying to implement a displayPerPage (10, 25, 50, etc.) when displaying a list of results on an index page. However i'm having a hard time how to just append that to the url without replacing the other query strings.
Example:
I have a paginator to switch pages however, if i'm on page 5 for example and select display per page 10 results on my select dropdown, it will load the results but erase the page=5 from the url. How to I just append displayPerPage without erasing where I currently am on the paginator.
Thanks for any help.
I know its possible to do it without creating a custom paginator since I have done it on another project with laravel 4, but that was a while back and I can't seem to figure out how to do it again.
Assuming $objects is the list of the item you're paginating, on your view you can add your paginator like that:
{{$objects->appends(request()->query())->links()}}
and query strings like your requestPerPage will be carried
After battling with this issue, I wrote a url parser helper function in php to to find and replace certain things in url after the ? sign (query part)
//for displayPerPage, orderBy and sortBy parse the url and replace with new values
function urlQueryParse($queryToReplace, $newValue){
// extract query from url
$fullUrl = Request::fullUrl();
if(strpos($fullUrl, '?') !== false){
//find everything after the ? in url
$url = substr($fullUrl, strpos($fullUrl, "?") + 1);
// check url to make sure $queryToReplace exists in the url
if(strpos($url, $queryToReplace) !== false) {
// look through the remaining url query and replace whats given to the function
$newUrL = preg_replace('/('. $queryToReplace. '\=)([^&]+)/', $queryToReplace.'='.$newValue, $url);
return Request::url()."?{$newUrL}";
}
// if the ? exists but the queryToReplace is not in the url, then add it to the query string
return Request::url()."?{$url}&{$queryToReplace}={$newValue}";
}
//if the url doesnt have ? sign then simply insert the new value
return Request::url()."?{$queryToReplace}={$newValue}";
}
Hopefully this helps someone. If you have any ides of how to improve it, I'm open to suggestions.
P.S. I'm working on Laravel 5.2

How to encrypt product ID in URL

I have a database with just over 800 data.
product table
pid name p_page
1 money money.php
2 gold gold.php
3 .
. .
. .
800 .
I have 2 pages...
product_item.php
<div class="button">
View
</div>`
when you click view the product info is pass to product.php
in here i have
if (isset($_GET['pid'])) {
depending on what product the user clicked on the URL might look like something below but the 44 will change to whatever id
http://www.example.x10.mx/money.php?pid=44
the problem with this, is that money.php have a different layout to the other pages and if I change 44 to 68, the product info will show on the page but the layout will not look good.
My question
what is the best way for me to stop users from being able to change the url.
I want to encrypt all my pid in the url so it will look something like
http://www.example.x10.mx//money.php?sel=the product name here or 4 letters or anything
I just want to take away pid from the url.
Please help me. If you dont understand my question please ask in the comment and try and say what you think you understand.
Edited to show my fetch function
$php = "php/";
$apages = "account/";
$bpages = "booking/";
$gpages = "general/";
$ppages = "product/";
// Global functions
function fetchdir($dir)
{
$protocol = $GLOBALS['protocol'];
$host = $GLOBALS['host'];
($dir == $GLOBALS['apages'] || $dir == $GLOBALS['bpages'] || $dir == $GLOBALS['ppages'] || $dir == $GLOBALS['gpages'] ? $branch = $GLOBALS['pagebranch'] : $branch = $GLOBALS['branch']);
echo $protocol.$host.$branch.$dir;
}
Thanks
p.s. I dont know if this can be done in .htaccess but i think it can be done in php
Some clarification:
I have a url which looks like this
www.example.com/account/product.php?pid=1
the problem with this is that someone can change 1 or any number and if they is a pid in the database with that number it will get the items information and display on the page. Which I don't want to happen because not all product are meant to be display in some pages.
In the papge which i show all my available product. I simple uses a SELECT statement and then echo what I need in some div.
In that page I have a view button.
$stmt = $conn->prepare("SELECT * FROM Product WHERE Type = 'shoes'");
$stmt->execute();
$i = 0;
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$id = ($row['pid']);
$product_page ($row['dir_page']);
<div class="button" >
<a href = "<?php fetchdir($apages) ?><?echo $product_page?ProdID=<?php echo $id>" > View</a >
</div >
}
Depending on the page that information is getting sent to when you click on view I use Get method
<?php
if (isset($_GET['pid'])) {
// Connect to the MySQL database
dbconnect();
$id = preg_replace('#[^0-9]#i', '', $_GET['pid']);
}
If you notice in my select statement used type to show only the product which type is shoes. I have other types as well, which as their other pages. Now the problem is if i change the pid to any page that doesn't have a type of shoes or if an in the other pages and enter a pid which type is shoes or anything, the information from that page will still render. Which I don't want to happen.
My question
how can i stop users from changing that pid and even if they change it. they will still be on the same page?
The problem isn't having the PID in the URL, it is having the template name in the URL.
Store the template name in the database (you are doing this already), and use that to determine what HTML to wrap the data in instead of putting it (money.php) in the URL.
Move your templates out of the web root (they shouldn't be hit by users directly), have a single index.php and then include() the template based on the data in the database.
You cannot prevent someone from changing the URL or from requesting arbitrary URLs. Your server (i.e. your app) has to decide how to respond to an invalid request. If you don't want to display certain things publicly, flag them as such in your database, test for that flag and simply refuse to output anything if that flag is hit.
Make the server respond negatively if something doesn't fit your conditions; don't expect the user to behave correctly.
Assuming that PID is a autoincrement value, you can still obfuscate it. Add another column in the table that contains a randomly-generated key (using uniqid or some derivative). Then use that key in your URL. You'll get something like: www.example.com/account/product.php?pid=II8GypI6H93Ij. This doesn't guarantee that someone won't find it, but it's good enough in most instances.
Check for allowance in the Database
Depending on your level of programming skills, in the database you could add a field or a relational table that relates the ID of the pages to allowed page templates (I'm guessing you're talking about templates.)
Then in the code you can make it so the page checks this database to see if the page contents are allowed to show. Something like:
$query1 = "SELECT * FROM Product WHERE Type = 'shoes' and allowedTemplate='1'";
This way you won't have to hardcode everything into the code itself. On the backend (if there is a CMS) then you could have checkboxes indicating the relationships to the templates and prefill them by default.
You'll need to make the site so something with that stuff though.
Your other option
You could use clean urls (which used to be better for SEO) to show real words instead of the IDs. Then you can use .htaccess tricks to convert the URIs to their ID counterparts with a dynamic RewriteMap.

Button link to Simple XML

OK, I have a simple xml call which retrieves data via an api, please see code below:
$file = 'http://mywebsite.com/computers.xml?apikey=88888&cid=api&limit=5&page=1 ' ;
if(!$xml = simplexml_load_file($file))
exit('Failed to open '.$file);
$total_pages = $xml->results->attributes()->totalPages;
$current_page = $xml->results->attributes()->currentPage;
$total_results = $xml->results->attributes()->totalResults;
foreach($xml->computer as $computer){
echo $computer['name'];
this all works perfect and breaks down the total number of pages, results and current page. With an if else statement i can generate next and previous buttons, however if i include the link to the api, when i click on it it simply calls the page and show the results as an xml format.
When what i want to achieve is yo provide a button which simply calls the second page of these results, i.e
$file = 'http://mywebsite.com/computers.xml?apikey=88888&cid=api&limit=5&page=2
however just clicking on this link shows the page in xml format, so what i need to do is to click on a button and retrieve the info via the simple xml function, however i do not know if this is possible or how to go about it.
And so any advice would be appreciated.
Thanks
You need to render a button that links to it's own url, plus a query parameter.
// get the param
$pageNum = $_GET['page'];
// modify your api call query string
$file = '...8888&cid=api&limit=5&page=' . $pageNum;
then your next button would link to
...href='ownpage.php?page=' . &pageNum + 1
and prev
...href='ownpage.php?page=' . &pageNum - 1
and I suppose your default page in the case where the page parameter is not supplied, you would default $pageNum to 1.

My PHP Random page generator has stopped working properly after 100,000 hits

My website relies completely on a random page generator that loads a page from a text file list. The code was kindly written by "lserni" on the forum. The script has been working perfectly the last few days, and it's happily processed over 100,000 page views in 3 days!
I noticed today however that it seems to have stopped working properly. If you are a brand new visitor to the page, or you've cleared your internet cache/cookies etc - When you load the page for the first time, it doesn't randomly generate a page.. it just shows a BLANK page. If you then refresh the page, the script works perfectly. I just can't get my head round it, but it's now resulted in a large drop in traffic! Hope you can help:
<?php
session_start();
if (!isset($_SESSION['urlist'])) // Do we know the user?
$_SESSION['urlist'] = array(); // No, start with empty list
if (empty($_SESSION['urlist'])) // Is the list empty?
{
$_SESSION['urlist'] = file("linklist.txt"); // Fill it.
$safe = array_pop($_SESSION['urlist']);
shuffle($_SESSION['urlist']); // Shuffle the list
array_push($_SESSION['urlist'], $safe);
}
$url = trim(array_pop($_SESSION['urlist']));
header("Location: $url");
?>
It's actually the LAST item in the file that's used first if there is no session data.
{
$safe = array_pop($_SESSION['urlist']); // gets item at the END of the array
shuffle($_SESSION['urlist']);
array_push($_SESSION['urlist'], $safe); // puts item at the END of the array
}
$url = trim(array_pop($_SESSION['urlist']));// gets item at the END of the array
So if you introduced a newline in your textfile at the end, it may be your issue.
I would suggest, after the header call, add some HTML that explains where the user is being redirected to. All being well nobody will ever see it, but it could help diagnose why the user gets an empty page.

Categories