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.
Related
<?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
I'm trying to display items a specific way...
First I want to check if $MYCAR_model exists (this is a session value). If not, do nothing.
Next I want to make sure that the URL variable cat is set to either 1 or 2. If not, also do nothing.
The URL would look like this...
http://www.mysite.com/?cat=1
My failed code...
if (isset($MYCAR_model) && ($cat=='1' || $cat=='2')) {
// show stuff
}
Thank you for any help you can provide.
Maybe
if (isset($_SESSION["MYCAR_model"]) ...
You can also use $_REQUEST["cat"] instead of $_GET["cat"] so that you don't need to worry about the parameter being passed via GET/POST/COOKIES.
if (isset($_SESSION['MYCAR_model']) && ($_GET['cat'] =='1' || $_GET['cat'] =='2')) {
// show stuff
}
I'd like to create a link that changes a PHP $_GET variable. For example:
URL: http://site.com/index&variable=hello&anothervariable=dontchangeme
Click me
(after click)
URL: http://site.com/index&variable=world&anothervariable=dontchangeme
I know you can do this to just change the page (href="1.html"), but I'd like to do the same thing while maintaining the GET variables that were already there.
$query = array('variable' => 'world') + $_GET;
printf('Click me', http_build_query($query));
See http://php.net/http_build_query. That's the easy to understand minimal version. Correctly you need to also HTML-escape the generated query string (because you're putting it into HTML):
printf('Click me', htmlspecialchars(http_build_query($query)));
You can simply redirect the user changing the variable value and using header()..
if(isset($_GET['variable'] && $_GET['variable'] == 'hello') {
header('Location: http://site.com/index&variable=world');
exit;
}
this should do it.
Click me
the variable or parameter in a url is preceded with a ? and then they are separated by &.
To get what you want just use this link:
Click me
but this is hardcoded and not dynamic in the sense that you can change the value of the parameter dynamically so my answer is probably not the best.
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");
}
}
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... ;-)
}