PHP Referal Error - php

Im trying to make a php refferall system, Some of my current code is below. But when i go to the url it says page not found? Is there a way to avoid that.
<?php
if (substr($_GET['page'], 0, 9) === 'referall/')
{
//Heres where it says page not found
$ref = substr( $url, strrpos( $_GET['page'], '/' )+1 );
//So i go to say http://example.com/referall/Me and it says not found
//The get ['page'] is the / of the website like http://example.com/index
}
switch ($_GET['page'])
{
case "login": { //Index page }
}
?>

If you want to achieve that, then you cannot use your URL in a way you are using:
example.com/referall/Me
bacause GET arguments are not passed to your code
but rather then use this URL:
example.com?page=referall&person=Me
and it shoud work.
If still you want to do that in a way you imagined, consider to use some framework that can make things easier for you, there are many of them, personally I recommend Codeigniter

Related

PHP to JS. Comparing code. Uncertain of purpose

<?php
$sPage = $_GET["p"];
//echo ("You picked the page: " . $sPage);
if ($sPage == "") {
$sPage = "home.php";
}
include($sPage);
?>
It came from a php multipage website. I would like to write this same kind of code, but in javascript.
What does this code do?
http://www.tropicalteachers.com/web110/superduper/
this link is where the code came from, the php dynamic one
Okey so let's just start from the top to the bottom. I will try to explain shortly what each php thing does also incase you don't know PHP to well.
$sPage = $_GET["p"];
This code above is getting query parameters that you got in your URL, currently it's getting the query parameters "p" so for example if the url was http://localhost/index.php?p=hola the "$sPage" variable would hold the value "hola".
if($sPage == "") { $sPage = "home.php"; }
Short if statement checking if there was a query parameter with a value, if not we will set the variable value to "home.php"
include($sPage)
So this will litrally just take the file "home.php" in this case and include it in page. So anything that is in the file "home.php" will be displayed on the current page you are at.
To replicate this in javascript it would be similar to using ajax to fetch the content you wanna display. Below i will link to a tutorial that can explain how to accomplish that.
https://www.w3schools.com/jquery/jquery_ajax_load.asp
This doesn't help with the URL part, but that you can google yourself to with the correct termanology

How to handle ?_escaped_fragment_= for AJAX crawlers?

I'm struggling to make AJAX-based website SEO-friendly. As recommended in tutorials on the web, I've added "pretty" href attributes to links: контакт and, in a div where content is loaded with AJAX by default, a PHP script for crawlers:
$files = glob('./pages/*.php');
foreach ($files as &$file) {
$file = substr($file, 8, -4);
}
if (isset($_GET['site'])) {
if (in_array($_GET['site'], $files)) {
include ("./pages/".$_GET['site'].".php");
}
}
I have a feeling that at the beginning I need to additionaly cut the _escaped_fragment_= part from (...)/index.php?_escaped_fragment_=site=about because otherwise the script won't be able to GET the site value from URL , am I right?
but, anyway, how do I know that the crawler transforms pretty links (those with #!) to ugly links (containing ?_escaped_fragment_=)? I've been told that it happens automatically and I don't need to provide this mapping, but Fetch as Googlebot doesn't provide me with any information about what happens to URL.
Google bot will automatically query for ?_escaped_fragment_= urls.
So from www.example.com/index.php#!site=about
Google bot will query: www.example.com/index.php?_escaped_fragment_=site=about
On PHP site you will get it as $_GET['_escaped_fragment_'] = "site=about"
If you want to get the value of the "site" you need to do something like this:
if(isset($_GET['_escaped_fragment_'])){
$escaped = explode("=", $_GET['_escaped_fragment_']);
if(isset($escaped[1]) && in_array($escaped[1], $files)){
include ("./pages/".$escaped[1].".php");
}
}
Take a look at the documentation:
https://developers.google.com/webmasters/ajax-crawling/docs/specification

PHP - load .php page of a particular get[] variable into a particular div?

I know this is an easy one but it's been a long time since i've done php.
I have 2 types of links. one using $_get[] of p, and other g.
like:
<a href='?p=pages/page-type-1'>page 1</a>
<a href='?g=pages/page-type-2'>page 2</a>
after clicking on a particular link , I want to load .php pages in divs of particular ids through the include('') method.
Thank you so much.
if($_GET['p'] === 'pages/page-type-1') {
include('page-to-include.php');
}
Whatever you do, DO NOT pass variables from $_GET to include. This is extremely dangerous! You cannot trust data being supplied in $_GET and so you always want to test it specifically and avoid passing it into other code unless you have to.
Also notice the use of === to force both a value check AND a type check. Look up === operator on PHP.net if you aren't familiar with this.
You said that you wan to load the pages in a particular div ids right? You can achieve this by using AJAX..
In your .js file write like this.
$.ajax({
type:POST,
url:'path/of/your/php/file/for/requesting/data.php',
success:function(data){
var get_data = parseJSON(data);
//Target the specific div you want to display your requested data that returned to you by your php.
$('#divid').html(get_data.name);
}
});
This should work:
<?php
if(isset($_GET['p']) && ($_GET['p'] === pages/page-type-1){
require_once 'some.php';
}
If you want to do anything with your GET data sanitize it first.Never trust your user
Sorry if this isn't quite your question, but a solution could be just use 1 GET variable for both pages, and then use a switch to determine which page you want:
if (isset($_GET["p"]))
{
switch ($_GET["p"])
{
case "pages/page-type-1":
include("page-type-1.php");
break;
case "pages/page-type-2":
include("page-type-2.php");
break;
default:
include("page-not-found.php");
}
}

Perform action URL is dynamically generated in PHP

I don't know how to easily describe this, here goes nothing...
I am writing a PHP script to perform an action if the URL is a specific URL, however, the URL is /ref/[VISITOR_USERNAME]. I want to set it so that anytime the URL is /ref/[ANY_TEXT], the action will perform.
if($_SERVER['REQUEST_URI'] == '/ref/' . string . '') {
...perform action...
}
How do I tell the script that if the URL is /ref/ and anything following that to perform the action?
Also, I realize there are other, probably better ways to do this, but for the sake of what I am trying to do, I need to do it this way.
Thanks in advance.
if(substr($_SERVER['REQUEST_URI'], 0, 5) == '/ref/') {
...
}
If you want to check for more you can build a regex:
if(preg_match('/\/ref\/.+/', $_SERVER['REQUEST_URI'])) {
...
}
You may just want to revise your Conditional Statement like so:
<?php
// CHECK THAT THE REQUEST URL CONTAINS "ref/[ANY_STRING_AT_ALL_INCLUDING_SLASHES]"
if( preg_match("#ref\/.*#", $_SERVER['REQUEST_URI'])) {
// NOW,GET TO WORK CODER... ;-)
}

Including pages with $_GET

I want url's like index.php?showuser=512, index.php?shownews=317 for pages i get content from db... and for regular pages index.php?page=about and so on WITHOUT mod-rewrite.
Invision Power Board has urls like this. I have looked through their code but I can't figure out how they do it.
I could do it like this:
if (ctype_digit($_GET['shownews'])) include('shownews.php');
elseif (ctype_digit($_GET['showuser'])) include('showuser.php');
// regular pages
elseif ($_GET['page'] == 'about') include('about.php');
elseif ($_GET['page'] == 'help') include('help.php');
elseif ($_GET['page'] == 'login') include('login.php');
But this feels too messy.
Just curious how IPB does this. Is there a better way do to this? WITHOUT any mod-rewrite. Any one know? I doubt they do it like the above.
I can't do:
if (preg_match('/^[a-z0-9]+$/', $_GET['page'])) include('$_GET['page']');
Then I would get links like index.php?showuser&id=512 and that I dont like. (i know its not safe just showing the princip)
I like it this way, it's not the best but i like it so please be quiet about template engines, frameworks etc. Just be kind and answer my question... I just want to know how IPB does this.
Thanks
Tomek
I don't know how IPB does this, let's get that out of the way. But, this is how I would approach this problem:
First, I recognize that there are two kinds of GET parameters: page/identifier, and just page. These would get tested separately.
Second, I recognize that all all get parameters match their filenames sans the php-suffix, so we can use this to our advantage.
One of the most important things to remember is to never let GET-parameters affect our code unsanitized. In this case, we know which types of pages we can and want to show, so we can create a white-list out of these.
So, onto the pseudo-y dispatcher code:
$pagesWithId = array("shownews", "showuser", "showwhatever");
$justPages = array("about", "help", "login");
foreach ($pagesWithId as $page) {
if (isset($_GET[$page])) {
$id = (int)$_GET[$page];
include($page.'.php');
die();
}
}
if (in_array($_GET['page'], $justPages)) {
include($_GET['page'].'.php');
die();
}
// page not found
show404OrHandleOtherwise();
For pages you just use a simple array.
if (isset($pages[$_GET['page']])) include $pages[$_GET['page']];
For shownews=317 You could make a simple conversion in your app. Depending on how you want to prioritize page or shownews etc:
if (isset($pages[$_GET['page']])) {
include $pages[$_GET['page']];
} else {
$possiblePages = array_filter(array_intersect_key($_GET, $pagesWithId), 'ctype_digit');
if (!empty($possiblePages)) {
$id = reset($possiblePages);
$pageName = key($possiblePages);
$page = $pagesWithId[$pageName];
include $page;
} else {
//no valid pages
}
}
Note: page "names" are array keys, and the value is the path, file and extension to include. More customizable.

Categories