To manipulate URL based on coming URL by PHP - php

How can you manipulate the destination URL by the name of the starting url?
My URL is
www.example.com/index.php?ask_question
I do the following manipulation in the destination URL
if ($_GET['ask_question']) {
// Problem HERE, since if -clause is always false
if ( $login_cookie_original == $login_cookie )
{
include "/codes/handlers/handle_login_status.php";
header("Location: /codes/index.php?ask_question");
die("logged in - send your question now");
}
}

if (isset($_GET['ask_question'])) {
...
If you did a print_r() of $_GET you would see
Array
(
[ask_question] =>
)
which shows that ask_question is set, but is empty, so it tests false.

$location = "test.php";
if(isset($_SERVER['QUERY_STRING']))
{
header("Location:".$location . "?" . $_SERVER['QUERY_STRING']);
}
else
{
header("Location:".$location);
}

I think you want to replace that with:
if (isset($_GET['ask_question'])) {
Which will only be true if it's contained in the URL.

you could possibly check the $_SERVER['QUERY_STRING'] variable to see if it contains 'ask_question'
edit: fixed the typo

You can retrieve values after the question mark by using the $_GET super global. For the example ?ask_question=true.
//is ask_question true?
if($_GET['ask_question'] == 'true') {
echo 'ask_question is true';
} else {
echo 'ask_question is not true';
}
For variables without values (like ?hello), use $_GET in such way:
if(isset($_GET['hello'])) {
echo 'hello is there';
} else {
echo 'hello is not there';
}
You've asked a lot of very basic questions about PHP and you don't seem to have a grasp on how the language works. I suggest giving the documentation a good read before your next question.
PHP Documention Home
PHP: Variables From External Sources

Related

Why my cookies are not saving?

I have a problem with cookies. In my login script i have the following line of code:
if($_GET['keep'] == "true"){
setcookie('id',$id,time()+3153600);
}
The problem I'm facing is that the cookies are not saving at all ( not even if i don't quit the browser). I'm quite a beginer in this respect and I think I'm not doing it right.
EDIT:
If i print_r all the Cookies it only gives me PHPSESSID after the cookie is set. I printed on index.php and i set the cookie on login.php
SOLUTION: Cookies are saved by default with the path of the file they were created in. To change the path there is another atribute. So by setcookie('id',$id,time()+3153600,'/'); you make the cookie available for the entire domain.
There is no issue in your code
if($_GET['keep'] = "true"){
setcookie('id',$id,time()+3153600);
}
This will may cause to
No data passing to $_GET['keep']
Or if data passing $_GET['keep'] value in not Matched ("true").
Both Works then $id is empty in setcookie method
Improve your code
if(isset($_GET['keep']){
if($_GET['keep'] == "true"){
if(isset($id))
{
#all perpect
$cokkie_id = 'id';
setcookie('id',$id,time()+3153600);
echo "I'm Set. And My value is ".$cokkie_id;
}
else
{
echo "Opzz My ID is also empty";
}
}
else
{
echo 'Get method is Set. But Value is not "true". Actual value is '. $_GET['keep'];
}
}
else
{
echo 'I cant reach Get method Buddy';
}
I think you miss "=" sign
if ($_GET['keep'] == "true") {
if (!isset($_COOKIE['id'])) {
setcookie('id',$id,time()+3153600);
}
}
use isset or ==
if (isset($_GET['keep']) && $_GET['keep'] == "true") {
setcookie('id', $id,time()+3153600);
}else{
echo 'keep is empty';
}

FILTER_VALIDATE_URL and $_GET, copy string after $_GET parameter

I use:
if (filter_var($_GET['paste_here'], FILTER_VALIDATE_URL)) {
echo ???;
}
And I'd like as soon as user enters a site after .php?paste_here = that specific site to be displayed on echo. But I don't want how to print. Any ideas? Thanks a lot
You should check out if param exists, so:
if (isset($_GET['paste_here']) && filter_var($_GET['paste_here'], FILTER_VALIDATE_URL)) {
echo $_GET['paste_here'];
}

pass query string get value into url link

I am trying to pass in a $_GET variable from a query string and pass it into a link to another page that has an application on it.
A customer will be directed to my page and the url will have the variable name merchantid. I need to take that on the home page, and pass it to the application page.
I've got it displaying on the home page as a test, so I know how to get it. I just need to know how to pass it the application page.
<?php
if (empty($_GET)) {
// no data passed by get
echo "<a href='{site_url}application'>Application</a>";
}
else
{
// The value of the variable name is found
echo "<a href='{site_url}application?merchantid=" .merchantid ."'><Application></a>";
}
?>
My else link actually blows up currently.
Ok, here is my second try, with the same result. The link blows up when I pass in the merchantid into the url. Ex. www.mysite.com/?=merchantid=12345
<?php
if (empty($_GET)) {
// no data passed by get
echo "<a href='{site_url}application'>Application</a>";
}
else
{
if(isset($_GET['merchantid'])){$merchantid = $_GET['merchantid'];}
else{$merchantid = "DefaultMerchant";}
echo "<a href='{$site_url}application?merchantid=" .$merchantid ."'><Application </a>";
}
?>
Why your code is not working
You're not telling php that "merchantid" is a variable nor you're defining it.
Solution
Replace
echo "<a href='{site_url}application?merchantid=" .merchantid ."'><Application></a>";
With
if(isset($_GET['merchantid'])){$merchantid = $_GET['merchantid'];}
else{$merchantid = "";}
echo "<a href='{$site_url}application?merchantid=" .$merchantid ."'><Application></a>";
}
Updated code
<?php
$site_url = 'http://'.$_SERVER['HTTP_HOST'].'/';
if (empty($_GET)) {
// no data passed by get
echo "<a href='{$site_url}application'>Application</a>";
}
else
{
if(isset($_GET['merchantid'])){$merchantid = $_GET['merchantid'];}
else{$merchantid = "DefaultMerchant";}
echo "<a href='{$site_url}application?merchantid=".$merchantid."'>Application</a>";
}
?>
$_GET is an array indexed by whatever values are in the query string. For example:
http://sit.url.com?merchantId=12&foo=bar
would place the following in the $_GET array:
$_GET['merchantId'] = "12"
$_GET['foo'] = "bar"
You will want a block in your code to initialize a $merchantId variable based on the presence of those values from $_GET:
//folks commonly use ternaries for this:
$merchantId = (isset($_GET['merchantId'])) ? $_GET['merchantId'] : false
Which is a shorthand way of stating:
if (isset($_GET['merhantId']) {
$merchantId = $_GET['merchantId']
} else {
$merchantId = false;
}
As Angelo and C.Coggins mentioned, don't forget the "$" in front of your variable in php.
You either need to assign $_GET['merchantid'] to $merchantid first, or replace $merchantid with $_GET['merchantid'] unless you have register_globals turned on, which you really shouldn't use.
So either add this:
$merchantid = $_GET['merchantid'];
or use this:
echo "<a href='{$site_url}application?merchantid=" . $_GET['merchantid'] . "'><Application></a>";
Besides that, as others pointed out, your original code is missing a $ before the variable name.

How to include 2 checks in a if statement

Basically I am coding a script where it simply redirects the user to the destination page. And I want to be able to check if multiple websites are not equal to the value; if this is so, it will run a error, else it will proceed.
I can't seem to get this to work though, although I am sure there's a way to check multiple values.
<?php
$url = $_GET['site']; // gets the site URL the user is being redirected to.
if ($url != "***.co", "***.net")
{
echo ("Website is not valid for redirection.");
} else {
echo ("You are being redirected to: " . $url);
}
?>
You can make an array of items to check for and then check if the url is in the array:
if (!in_array($url, array("***.co", "***.net")))
{
}
You can also use multiple conditions like #wrigby showed, but the solution using an array makes it easier to add more (or a dynamic number of) urls. But if there are always two, his is better.
You'll need two complete conditionals, connected with a logical and (&&) operator:
<?php
$url = $_GET['site']; // gets the site URL the user is being redirected to.
if ($url != "***.co" && $url != "***.net")
{
echo ("Website is not valid for redirection.");
} else {
echo ("You are being redirected to: " . $url);
}
?>

How to check if an include() returned anything?

Is there any way to check if an included document via include('to_include.php') has returned anything?
This is how it looks:
//to_include.php
echo function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
include('to_include.php');
if($the_return_of_the_include != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
So after I've included to_include.php in my main document I would like to check if anything was generated by the included document.
I know the obvious solution would be to just use function_that_generates_some_html_sometimes_but_not_all_the_times() in the main_document.php, but that's not possible in my current setup.
make function_that_generates_some_html_sometimes_but_not_all_the_times() return something when it outputs something and set a variable:
//to_include.php
$ok=function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$ok='';
include('to_include.php');
if($ok != '') {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}
If you are talking about generated output you can use:
ob_start();
include "MY_FILEEEZZZ.php";
function_that_generates_html_in_include();
$string = ob_get_contents();
ob_clean();
if(!empty($string)) { // Or any other check
echo $some_crap_that_makes_my_life_difficult;
}
Might have to tweak the ob_ calls... I think that's right from memory, but memory is that of a goldfish.
You could also just set the contents of variable like $GLOBALS['done'] = true; in the include file when it generates something and check for that in your main code.
Given the wording of the question, it sounds as if you want this:
//to_include.php
return function_that_generates_some_html_sometimes_but_not_all_the_times();
//main_document.php
$the_return_of_the_include = include 'to_include.php';
if (empty($the_return_of_the_include)) {
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
} else {
echo $the_return_of_the_include;
}
Which should work in your situation. That way you don't have to worry about output buffering, variable creep, etc.
I'm not sure if I'm missing the point of the question but ....
function_exists();
Will return true if the function is defined.
include()
returns true if the file is inclued.
so wrap either or both in an if() and you're good to go, unless I got wrong end of the stick
if(include('file.php') && function_exists(my_function))
{
// wee
}
try
// to_include.php
$returnvalue = function_that_generates_some_html_sometimes_but_not_all_the_times();
echo $returnvalue;
//main_document.php
include('to_include.php');
if ( $returnvalue != '' ){
echo $do_a_little_dance_make_a_little_love_get_down_tonight;
}

Categories