Perform action URL is dynamically generated in PHP - 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... ;-)
}

Related

Using isset to display page content

I am having an issue using isset to display content on a page.
My PHP file is called messages.php
I am directing my users with links to this URL: messages.php?inbox using if(isset($_GET['inbox']))
{ } to display the users inbox. Same principle with the other users options such as compose message is: messages.php?compose again using isset
The only problem I have is that I cannot stop people from manually typing stuff like domain.com/messages.php or domain.com/messages.php?somethingrandom.
Is there a way to direct users to messages.php?inbox when they type in the address bar something that isnt assigned to isset?
I did try to use switch but couldnt seem to get it to work properly with how ive laid out my HTML.
An example of the whole file is here http://pastebin.com/SfqN2L7g
I am fairly new to PHP and think I may have gone down the complicated route.
Any advice would be appreciated.
Thanks
The answer you added already would work, but I usually like having an array of valid options which I could maybe check against later on.
$validPages = array('inbox', 'compose');
$pageFound = false;
foreach ($validPages as $validPage) {
if (isset($_GET[$validPage])) {
$pageFound = true;
break;
}
}
if (! $pageFound) {
header('Location: /messages.php?inbox');
}
Thanks to the help of Marcos PĂ©rez Gude, the answer is as follows:
if(isset($_GET['inbox']) || isset($_GET['compose'])){
//Then do below
}else{
header("Location: messages.php?inbox");
exit;
}

PHP Referal Error

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

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");
}
}

including the php page when two variable are set

hello everyone please help me about this problem
i am sending two variable to the main page and now i want to put the check that if these twi variable are true then called the specific page . here is the sample code
http://localhost/admin/admin.php?page=add_user?view=3
now i want when page and view are set then call a specific page like this
if(isset($_GET['page'])=='add_user' && $_GET['page']=='add_user' ) include("user/add_user.php");
in short i want to call this add_user.php when two variable are set up so please suggest for the above line
<?php
// the url would be http://localhost/admin/admin.php?page=add_user
if(isset($_GET['page']) && $_GET['page']=='add_user'){
include("user/add_user.php");
}
?>
if(($_GET['page']=='add_user') && ($_GET['view']=='3')){include("user/add_user.php");}
but in your GET request, separate variable with &, not a second ?
Use double switch
Pseudocode:
switch($_GET['page'])
..
case: 'test':
switch($_GET['view']){
...
}break;
...
If all of the php files are named after the get request. You could do something like:
if((isset($_GET['page'])) && (isset($_GET['view'])))
{
require_once('user/'. $_GET['page'] . '.php');
}
For obviousness: It'd still need to be escaped, or filtered to see if nothing "bad" is being requested.

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