Redirecting by Header in php - 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");

Related

How to remove %0D%0A from end of a parameter in PHP

I am trying to hit a URL after generating the data to be filled for the parameters that are passed in URL using Python in back end. So the flow is:
User lands on a page with a form having some drop downs.
Python code in the backend reads the content from a file and returns single output based on some conditions for each of the dropdown.
User hits the submit button with the data.
The data gets generated correctly but when I hit submit button, I get %0D%0A characters at the end of the parameter values in the URL
E.g., sample.php?param1=20%0D%0A&param2=50%0D%0A
How do I get rid of these values as this is causing trouble with the other code where I am using these values?
I take it you read the data from a file, so probably reading the file causes the line endings to be read as well.
In any case, try using strip() or rstrip() in your Python code to remove all/trailing whitespace before your assemble the target URL.
I understand that it's actually a PHP script that assembles the URL. In that case, use PHP's trim() function on the variables you use to assemble the URL.
For example: Assume that $val1 and $val2 are read from a file or some other place. Then the following line assembles above URL stripping whitespace from $val1 and $val2.
$url = "sample.php?param1=" . trim($val1) . "&param2=" . trim($val2);
Some browsers do that automatically, you can try decoding it back using urldecode()
http://php.net/manual/en/function.urldecode.php
Try this :
<?Php
$str = "Your Inputed Value or string"
$url = str_replace(" ","-", $str);
?>
Link Menu

Page name as a php variable

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

variable linked in page URL, how-to and are spaces okay?

3 quick questions:
I want to create one page "view_build.php" which, when opened, takes a variable from the URL contained within it, and displays unique information for that 'build'. For example
$row[0]
The above is a link to a page called view_build.php?buildname=VARIABLE
Does the .php file contain the variable in a $_GET array, JUST from me including a variable name in the html link?
And if so, is it okay if there are spaces in it? For example:
view_build.php?buildname=Dual Zoren
Lastly, how do I include more than one variable in the URL? What is the syntax?
Thanks so much!
Regards.
Does the .php file contain the variable in a $_GET array, JUST from me including a variable name in the html link?
Yes. To elaborate, if you call view_build.php via an HTTP request with the URL query parameters foo=bar, then within that script, you can access $_GET['foo'] which will have the value bar.
is it okay if there are spaces in it?
No, they should be URL encoded. See http://php.net/manual/function.urlencode.php
For example
<?= htmlspecialchars($row[0]) ?>
Lastly, how do I include more than one variable in the URL? What is the syntax?
URL query parameters are separated by the & character, eg
view_build.php?buildname=Dual+Zoren&foo=1&bar=2

$_GET variable in php without using a form

i have gallery.php?gid=1 in gallery.php page and i have to submit data to save.php without using a form.
and i want to get the value of gid without using a button..
is that possible?
gallery.php
$gid=$_GET['gid'];
save.php
echo $gid..
header("location:save.php?gid=".$gid);
OR
use a anchor tag with href="save.php?gid=<?php echo $gid;?>"
$_GET will contain all the variables which are in the URL, after the "?" (foo.php?gid=...).
You are not obliged to use a form to fill these variables.
The members of $_GET array is got from query string. In a simple way, a query string is tha part after the ? character of the URL and formatted in this way:
param1=value1&param2=value2&....
So you can pass any pair of key-value in to your PHP script by adding a query string to the end of this PHP file URL.

How do I make redirection dynamic

Please i need your help with a small issue i'm having. I'm using the Redirecting users to the page they have just pasted a comment on, and d=3 is suppose to be dynamic i.e take different intergers according to the id .
I've tried to add the php variable like this
Header('Location: http://site/a.php?id=$articleID')
but i get an error.
Header('Location: http://site/a.php?id=3')
Please how can I get over this issue.
Thanks
You can't pass variables into single quoted strings
header("Location: http://site/a.php?id=$articleID");
exit;
don't forget to include exit;, otherwise the rest of the script will still execute until the new page is loaded.
Change your quotes from single to double. Single quotes won't interpret variables inside them.
Header("Location: http://site/a.php?id=$articleID")

Categories