i am new to php, but im trying. i need you guys help.
i have the following url in the browser address bar www.dome.com\mypage.php?stu=12234342
i am trying to pass the url from the main page to the select case page call select.php
if i should echo the url i get www.dome.com\select.php. so i have decided to echo $_SERVER['HTTP_REFERER']
instead, this gives me the correct url. how can i echo the variable from www.dome.com\mypage.php?stu=12234342 (12234342)
unto select.php. select.php contains code that needs the $var stu=12234342 in order to display the correct message.
$request_url=$_SERVER['HTTP_REFERER'] ; // takes the url from the browers
echo $request_url;
$cOption = $_GET['id'];
switch($cOption) {
case 1:
echo ' some text';
break;
case 2:
echo ' this page.php';
break;
case 3:
echo 'got it';
break;
default:
echo 'Whoops, didn\'t understand that option: <i>'.$cOption.'</i>';
}
?>
You may use parse_url() and parse_string() to grab the variable from a url:
<?php
//assuming www.dome.com/mypage.php?stu=12234342;
$url=$_SERVER['HTTP_REFERER'];
//parse the url to get the query_string-part
$parsed_url=parse_url($url);
//create variables from the query_string
parse_str($parsed_url['query'], $unsafe_vars);
//use the variables
echo $unsafe_vars['stu'];//outputs 12234342
?>
But note: you can't rely on the availability of HTTP_REFERER.
try
echo $_GET['stu'];
on select.php
That's why you need to call the select.php file like this:
www.dome.com/select.php?stu=12234342
and then you can add:
echo $_GET['stu'];
By the way, you need to research about XSS, because that's a huge vulnerability.
Related
I'd like to replace content within my page based on the URL parameter.
Ideally I'd like to use PHP to get:
if {{parameter is X}} display {{content X}}
if {{parameter is Y}} display {{content Y}}
..for a few pages.
Current set up:
<?php if ($CURRENT_PAGE == "Index") { ?>
<div id="firstDiv">this is the standard page</div>
<?php } ?>
<?php if ($CURRENT_PAGE == "p1") { ?>
<div id-"secondDiv">this is a variation of the page</div>
<?php } ?>
And using include("includes/content.php"); to call the html blocks to the page
The firstDiv displays in index.php as expected, but adding the URL parameter changes nothing - the same div still shows (I'd like it to be replaced with the secondDiv)
It seems $CURRENT_PAGE doesn't like URL parameters - what is the alternative?
Hopefully this makes sense, I'm pretty new to PHP. Happy to provide more details if required.
Thanks in advance for any help.
-- UPDATE --
Thank you for the answers so far!
It seems I missed part of my own code (Thanks to vivek_23 for making me realise this - I'm using a template, excuse me!!)
I have a config file that defines which page is which, as so:
<?php
switch ($_SERVER["SCRIPT_NAME"]) {
case "index.php/?p=1":
$CURRENT_PAGE = "p1";
break;
default:
$CURRENT_PAGE = "Index";
}
?>
Before I learn $_GET, is there a way I can use my current set up?
Thanks again.
-- UPDATE 2 --
I have switched to using the $_GET method, which seems to be working well so far. My issue now is when the parameter is not set it is giving an undefined error. I'll try to remember to update with the fix.
$p = ($_GET['i']);
if($p == "1"){
echo '<div id="firstDiv"><p>this is the first div</p></div>';
}
Thanks to the two answerers below who suggested using $_GET
You can used $_GET like
if($_GET['p']==1){
echo '<div id="firstDiv">this is the standard page</div>';
}else if($_GET['p']==2){
echo '<div id="secondDiv">this is a variation of the page</div>';
}
The other way! you can used basename() with $_SERVER['PHP_SELF']
//echo basename($_SERVER['PHP_SELF']); first execute this and check the result
if(basename($_SERVER['PHP_SELF']) == 'index'){
echo '<div id="firstDiv">this is the standard page</div>';
}else{
echo '<div id="secondDiv">this is a variation of the page</div>';
}
You need to send the parameters on the URL query string, like:
yourdomain.com?p=1
So, with this URL, the query string is "?p=1", where you have a GET parameter named 'p' with a value of '1'.
In PHP to read a GET parameter you can use the associative array $_GET, like this:
$current_page = $_GET['p'];
echo $current_page; // returns '1'
The rest of your logic is OK, you can display one div or the other based on the value of the p parameter.
You can read more about how to read query string parameters here: http://php.net/manual/en/reserved.variables.get.php
i got the code below to find the url of the site and this works. But I want a switch statement to search this url in a list and echo something else in for every url.
$actual_link = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];
As far as I understand the question, this looks like what you are looking for:
switch ($actual_link) {
case 'http://domain/something1/':
echo 'Something 1';
break;
case 'http://domain/something2/':
echo 'SomeSthing 2';
break;
default:
echo 'default';
}
I have a link, an offer page and a destination page. I need to carry the variables from the original link and input them into the links on the offer page.
original link
www.example.com/offerpage.php?offer=1&aff_id=var1&aff_sub=var2
Where you see var1 and var2, those could be any number.
I'm assuming I could do something like this (this is a total guess, just want to make sure I do it correctly).
<?php
if(array_key_exists('aff_id', $_GET)){
$aff_id = $_GET;
}
else {
$aff_id = '1';
}
?>
Then the links on the offer page would be
www.offer.com/index.php?offer=1&aff_id=<?php echo $aff_id; ?>&aff_sub=<?php echo $aff_sub; ?>
and whats the correct format for doing multiples?
This should probably do what you want:
if (!array_key_exists('aff_id', $_GET)) {
$_GET['aff_id'] = 1;
}
echo http_build_query($_GET);
If the query string is offerpage.php?offer=1&aff_id=var1&aff_sub=var2then the output will be:
offer=1&aff_id=var1&aff_sub=var2
And, if the query string doesn't contain aff_id, i.e. offerpage.php?offer=1&aff_sub=var2 then the output will be:
offer=1&aff_sub=var2&aff_id=1
I have:
one
And:
two
PHP page:
<?php
if mypage=one then
echo "my stuff text........one";
if mypage=two then
echo "my stuff text........two";
?>
I want to get text separately for each link from same php page
First of all, if then construct is not available in PHP so your code is syntactically wrong. The use of switch as suggested already is a good way to go. However, for your problem, you should use $_GET['mypage'] instead of $_POST['mypage']. It seems you are beginning PHP. Once you get some good basics, you will probably be making use of the functions such as include() and require(). Make sure you do not make mistakes beginners do:
<?php
if (isset($_GET['mypage'])
{
#include($_GET['mypage']);
}
?>
The above code works and looks simple but it has a very dangerous implementation allowing the malicious users to perform an attack known as file inclusion attack. So you should try to use the switch statements such as:
<?php
$mypage = $_GET['mypage']; //also you might want to cleanup $mypage variable
switch($mypage)
{
case "one":
#include("one.php");
break;
case "two":
#include("two.php");
break;
default:
#include("404.php");
}
?>
Umm, that php is not even remotely valid code. You want a switch statement:
<?php
$mypage = isset($_GET['mypage']) ? $_GET['mypage'] : '';
switch ($mypage) {
case 'one':
echo "my stuff text........one";
break;
case 'two':
echo "my stuff text........two";
break;
default:
header('HTTP/1.0 404 Not Found');
echo 'This page does not exist';
}
I feel like the following script file should work for navigation on my site, but when I click around on the links nothing loads up and nothing loads by default. How do I fix it?
<html>
<head><title>Your Title</title></head>
<body>
Navigation:
News
Whatever1
<br /><br />
<?php
$id = $_GET;
switch($id)
{
default:
include('home.html');
break;
case "what1":include('whatever1');
break;
case "what2":include('whatever2');
}
?>
</body>
</html>
What are you $_GETing? Also $_GET returns an associative array, and the switch statement takes a variable. You need to specify what it is you get by giving it an id like $_GET['id'].
You need to go in to the $_GET variable and pull out the exact field you want:
$_GET['id']
You don't store the actual GET variable in $id,
$id = $_GET;
should be
$id = $_GET['id'];