PHP to JS. Comparing code. Uncertain of purpose - php

<?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

Related

Is it possible to output certain content on a php document and make it die only if it’s URL is present with a value of true on a MySQL database?

I am trying to setup a PHP document but I currently am looking for a way to use the die() function and display some content on every page using my global configuration file. The way I am think how it should work is that IF the requested URL (e.g. domain.com/services/disabledservice) would have /services/disabled service and a value of 1 to make the value true in a MYSQL DB.
The plan is to have the URL be collected and checked with a table than if the row has a value of 1 for status it will display a disabled page message but if it’s 0 it will load normally.
Some research I have conducted may lead be to think that using the SQL query and the if function could work for this.
The idea I have is this but it may not be correct.
<?php $pageurl = [requested URL content here]
$checkstatus = "SELECT * FROM servicestatus WHERE page =" . $pageurl . "AND status = 1";
if ($status = mysqli_query($conn, $servicestatus)) {
if (mysqli_num_rows($status) = 1) { ?> html content here
<?php }
} else { ?>
page as normal
<?php } ?>
Edit:
To explain what I am trying to do.. I am trying to fetch the URL without everything past “?” Than I am trying to use that in a DB query to check with the database if that has a value of “m” or “d” and if it has one of those values next to the URL which is being fetched it will display the appropriate error page. This is being included as part of my core configuration file which includes my “$conn” and the core values for most stuff. The problem I am facing is that when I send my URL without everything past the “?” I am not receiving my error page and everything is loading like normal.
Use any one the following php functions:
include 'path_to_the_page.php' (or) require 'path_to_the_page.php';
The difference between include and require arises when the file being included cannot be found: include will emit a warning ( E_WARNING ) and the script will continue, whereas require will emit a fatal error ( E_COMPILE_ERROR ) and halt the script.

Prevent end user manipulating URL to change content on website, method GET PHP

I have a personal search site project I'm building, at the moment the only data that is being displayed on the website is data that is retrieved using SELECT queries and the GET method using the super global $_GET['example']. Now I don't know if I'm doing this wrong but some parts of my page are only displayed if certain GET variables in the URL are set or not empty. Below shows how my URL looks
EXAMPLE: index.php?search_category=guitar&main_category=9&postcode_val1=NP22&distance_default=100&submit=search
I have a lot of these if(isset($_GET['search_category']) type conditions in my website which are replied upon and show particular parts of content depending whether or not these are either true or false.
I have been on a lot of other websites that have similar URL's, I have tried to alter and manipulate these and the content does not break, alter or change in any way yet when i try this with my url it breaks my page and only certain parts of content gets displayed by being based on what is set. Is there some other layer of protection I should add, would using something like a rewrite rule help? The code below shows how I have wrote a drop down box based on what has been set In the URL but if a user edits the URL this is easily broken.
if(isset($_GET['search_category']) && isset($_GET['main_category']) &&
isset($_GET['postcode_val1']) && isset($_GET['distance_default']))
{
$stmt = $getFromUi->dispCategories();
echo "<option value='0'>All</option>";
echo "<option value='#'>-------------</option>";
while($row = $stmt->fetch(PDO::FETCH_OBJ))
{
$selected = '';
if(!empty($_GET['main_category']) && $_GET['main_category'] == $row->cat_id)
{
$selected = ' selected="selected"';
}
echo '<option value="'.htmlentities($row->cat_id).'"'.$selected.'>'.htmlentities($row->cat_title).'</option>';
}
}
It will break because the strict nature of logic you use on your code. The && mark with isset mean any parameter you define not set will not evaluate to true. If the parameter is quite flexible why not ||.
If you need it to still evaluate all parameter try to do limit first if condition to main determiner. like $_GET['search_category'] and use the remaining $_GET['other_parameter'] as needed inside the block code of main if.
You would need to use a post method, so that this goes through as a request instead. In my experiance, get will only fetch the url you open - not actually pass anything through unless its in the URL.
Not sure if that made any sense, but check post out.
https://www.w3schools.com/tags/ref_httpmethods.asp is a good place to start to see the difference of get vs post.

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

Using WildCard in PHP if search program

this is my code that I've written so far --
<?php
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$search_link = "http://www.example.com/register_complete.php?user=";
if($actual_link == $search_link)
echo "Place your conversion pixel code here";
?>
The situation is like this - Once a user completes a registration he/she lands on urls like these -
http://www.example.com/register_complete.php?user=abcd1234
http://www.example.com/register_complete.php?user=abcd12345
http://www.example.com/register_complete.php?user=abcde123456
So what I am trying to achieve is fetch the current url using $_SERVER and then matching it with my sample url which is stored in $search_link if both these match then I wish to display a particular code otherwise the code will not be displayed anywhere else in the website.
I don't know how to use wildcard entries in PHP :(
My mind tells I should have done something like this -
http://www.example.com/register_complete.php?user=*
Can anyone over here help me regarding this please?
Why so complicated? I bet you can exclude http://www.example.com/register_complete.php, because if it's not this file, it won't load the script anyways, so focus on ?user=abc
if (isset($_GET["user"])) {
echo "Place your conversion pixel code here";
}
You can also try empty()
OR if you want to compare it to certain value:
if ($_GET["user"] == "abcd1234") {
echo "Place your conversion pixel code here";
}
There is a function which could help you with your problem and that is strpos(). Check out the following link:
http://in3.php.net/strpos

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

Categories