Page name as a php variable - php

I am working on a project that requires me to redirect the user to a php page. And the name of the page to be redirected to is stored as a php variable.This is what I tried. Suppose $var is the name of the php file. I want to do something like this,
if(condition)
{
header("Location: '$var'.php");
}
How do I do this?

You simply need to follow string concatenation here. Try this:
if (condition) {
header("Location: ".$var.".php");
}
Refer to String Operators

You have to concatenate the string which you have stored the page name into the header location tag so that then only it will read the name that you have stored.
More Clear Explanation: You have used double quoted string over to the header location and hence you need to close of with the double quoted string and then reopen again with the double quoted string alone. This might be the correct procedure for appending or concatenating it into the variables.
Example:
<?php
$page='index';
$page_one ='about';
?>
Hence you need to concatenate the variable as follows into the PHP tags.
<?php
if (condition) {
header("Location: ".$page.".php");
}
?>

Related

How to prevent PHP variables and expressions from expanding in string

The following text link works fine when I place it directly in my html:
Click here to <?php echo $showOrHideText; ?> the suggested sequence of lessons.
But I want to replace it with:
<?php echo $gradeNote; ?>
Elsewhere $gradeNote is assigned a string based on the grade of the student user. My question after many hours of searching and failing is how can I pass this snippet as a literal string, without PHP attempting to parse it and giving me a junk url? What am I doing wrong here:
$gradeNote = "Click here to <?php echo $showOrHideText; ?> the suggested sequence of lessons.";
You're running <?php and ?> tags inside of a PHP variable. As you're already dealing with PHP, these are unnecessary.
Although the quotation marks "" allow you to echo out evaluated variables, because you're also running a condition in this 'string', you'll want to extrapolate that out and simply store the result as a variable. I've called mine $show.
As such, you're simply looking for:
if($slcustom29 == 0) {
$show = 1;
}
else {
$show = 0;
}
$gradeNote = "Click here to $showOrHideText the suggested sequence of lessons.";
Remember to either escape the double-quotes in the <a href="">, or swap them for single-quotes.
This can be seen working here.
Hope this helps!
Try something like this.
$s = ($slcustom29 == 0) ? 1 : 0;
$gradeNote = "Click here to {$showOrHideText} the suggested sequence of lessons.";
Any string with double quotes "" can have a variable embedded, the {} are not necessary, but i always use them for cases like this where you are trying to embed a variable with no spaces around it, "$xabc" which will return a different result "{$x}ab"
the probelm is that you are trying to put php logic into the string. Notice you have an IF command within the string literal. start with a small or empty string, and concat onto it piece by piece, as opposed to doing it in one line.
then you can echo out the single variable link

Introducing a PHP variable into a Header redirect

this is my code:
header("Location: ?pid='".$_GET['pid']."'");
die();
When I write a simple echo $_GET['pid']; the value is good but then when I introduce this variable in the header it return something like 27%27 and thats not true true value
When I use urlencode the probleme persist:
header("Location: ?pid=". urlencode($_GET['pid']);
Whats the problem here?
Thank you
This is because the parameter is being encoded into URL format. Read about urldecode() PHP function.
Also, the %27 is a URL encoded single quote char, therefore you need to remove single quotes from your code:
header("Location: ?pid=".$_GET['pid']);
If you still however will get %27 in your header, then I would suggest stripping it out from var by using trim() like this:
header("Location: ?pid=".trim($_GET['pid'], "'"));

Redirecting by Header in php

I am using wampserver for php and mysql database.
in my php file, after executing some code to insert in mysql database, I want to redirect browser to another html page that has php code in it. This page needs value of the variable id to fetch data from my sql database. So I send value of id by below code in the end of php code.
header('Location: lostItem.php?id=$id');
The recieving file has below code to get value of id.
$id= $_GET['id'];
But, it turns out that there is no value of id passed i.e. the receiving file shows url :
http://localhost/Lost%20and%20Found/lostItem.php?id=$id
instead of showing
http://localhost/Lost%20and%20Found/lostItem.php?id=11
I already have another page that sends same data (value of id) to receiving file. It has below code in it for that.
echo "<a class='listOfItems' href='lostItem.php?id=$id'>";
echo $item;
echo "</a>";
And that works fine. But when I try to do same thing by using header, it doesn't work. Before using header, I have made sure that variable $id has right integer value. But it doesn't send that value by using header.
Is it that data can't be sent by this method using header? If so, please suggest an alternative method.
basic php notation, in single quotes variables are not interpreted
header("Location: lostItem.php?id=$id");
to be strict, location should use a full URI not a relative one
header("Location: http://www.example.com/lostItem.php?id=$id");
use curly bracket upon variable when you used variable value in string, Please try this
header("Location: lostItem.php?id={$id}");
Single quotes does not process variables, PHP ignores all your variables so you need to concatenate variable using concatenate operator (.)
header('Location: lostItem.php?id='.$id); // fast than double quotes
or
header("Location: lostItem.php?id=$id");

I want to PHP include a variable, doesn't seem to be working

I am trying to PHP include a variable from the database that contains a URL but it doesn't seem to be liking it, any ideas?
The code is
<?php
$location=$row_info['location'];
include '.$location./index.php'; ?>
Thank you for your help :-)
include '.$location./index.php'; ?>
should be
include ".$location./index.php"; ?>
Variable names are not expanded inside single-quotes. From the PHP Manual:
The most important feature of double-quoted strings is the fact that
variable names will be expanded
Try this:
include "$location/index.php"; ?>
Or this:
include $location.'/index.php'; ?>
Either of those should work better. PHP won't actually expand the $variable inside a '' quote, you'll just get $variable as text in the output instead so you want to put the $variable outside the quote using dot between them like the second example or use "" quotes like the first.
you have that a bit backwards...
$location = $row_info['location'].'/index.php';
include $location;
This should work or you could use double-quotes for values and get same results. As stated above. :)~

PHP Header Location with parameter

Is it possible to append a parameter to a PHP Header Location? I'm having trouble getting it to work. Is this syntax actually allowed?
$qry = $_SERVER['QUERY_STRING'];
header('Location: http://localhost/blast/v2/?$qry ') ;
it just won't replace $qry wit its actual value....why??
in the browser it ends up looking like this:
http://localhost/blast/v2/?$qry
thanks
Change the single quotes to double quotes:
header("Location: http://localhost/blast/v2/?$qry");
A single quoted string in PHP is treated as a string literal, which is not parsed for variables. Double quoted strings are parsed for variables, so you will get whatever $qry contains appended, instead of literally $qry.
You can also add multiple parameters via a header like:
$divert=$row['id']."&param1=".($param1)."&param2=".($param2);
header("Location:showflagsab.php?id=$divert");
which adds the two additional paramaters to the original id
These can be extracted using the $get method at their destination
I know this is a very old post, but please, do not do this. Parameters in a header are XSS vulnerable. You can read more about XSS'ing here: owasp.org/index.php/Cross-site_Scripting_(XSS)
wiles How to Fix the XSS attack on header location
$param = $_REQUEST['bcd'];
header("Location: abc.php/?id=$param");

Categories