PHP header appends to old url - php

I am trying to implement a feature where after a button is clicked, the server redirects the user to a new webpage with some variables in the url. I am running into a bug where the header function keeps appending to my existing url instead of refreshing it. EX:
http://localhost/455Project/messaging.php/new_message.php/?ssn=
messaging.php contains a button which when clicked redirects to new_message.php with a ssn attribute.
var_dump($_GET);
$temp = $_GET['patient'];
if(isset($_GET['new_message'])) {
$url = "new_message.php/?ssn=" . $temp;
header("Location: " . $url);
die();
}
The current url has the attribute which I want to get passed to the next. The var_dump has the correct value. I have also tried using "/455Project/new_message.php/?ssn=" which solves the url appending but in either instance the $temp variable doesn't append the proper value.
EDIT: I have a dropdown that produces the url like this echo "
`href='messaging.php/?patient=$patient_ssn_row[0]'>$name_row[0] $name_row[1]'</a>";`
This corresponds to a dropdown with data and the user selects one of the options.

Related

Redirect to another URL writing it inside the own URL

Very probably this is impossible, but I want to ask it, just in case.
I have a database in which I save some numbers (1, 2, 3...).
I have a .php page from which I read the numbers. I concat those numbers to a string for getting a full URL. For exmaple:
$intArticle = $row["article"];
$strURLBase = "example.com/index.php/"
$strLink = $strURLBase . $intArticle;
//And I get "example.com/index.php/1"
But now, there is an exception in which the URL points to an external website, so my code is not valid for now.
I know how to fix it in .php, but I would like to know if it is possible to make the redirect directly inside the URL, saving the properly string inside the database. For example:
$intArticle = $row["article"]; //In this case, the value of $row["article"] could be, for example, "http://www.externalweb.com"
$strURLBase = "example.com/index.php/" //This part should be ignore inside the URL
$strLink = $strURLBase . $intArticle;
//I would get "example.com/index.php/http://www.externalweb.com"
Is there any kind of instruction that I could write inside the URL (that I would save into the database and then I would concat to $strURLBase) that redirects to another URL? For example:
example.com/index.php/!$%&_redirec_to("http://www.externalweb.com")
I don't want to call any PHP function from the URL for the redirection. In fact any PHP code shouldn't be executed. Everything should be inside the URL.
Try this:
if (!is_numeric($intArticle))
{
header("Location: ".$strLink."");
exit;
}
// your site will continue here, if the article is a number

How to parse multiple GET parameters into URL if one unsets another?

Let's say, I'll have a webpage "abcde.com" with two submit buttons: "Set GET 1" and "Set GET 2". The "Set GET 1" button redirects user to abcde.com?first=x. The "Set GET 2" button redirects user to abcde.com?second=x. Both using PHP's ('Location: ?first=') and ('Location: ?second=').
If user clicks on Set GET 1, GET parameter will be set in the URL. However, if user clicks on Set GET 2 right after setting the first GET parameter, the button will redirect him to the second parameter, removing the first one from the URL.
Is it possible to achieve setting multiple GET parameters in the URL divided by & in PHP without writing if (isset($_GET["first"])) { ('Location: ?first=x&?second=x') }?
I mean, so it can add & and set another parameter without removing the previous one? Even though I understand that PHP redirects user to URL without previous GET parameter.
Use $_GET to store current query string in an array, append your values to it, then build the new querystring with http_build_query:
$queryArray = $_GET;
$queryArray['second']='x';
$urlWithQueryString = 'http://example.com/somepage/?' . http_build_query($queryArray);
header('Location:'.$urlWithQueryString);

Variable in string disappears when redirecting with header()

I have a SMARTY form and would like to pass a variable(set from the url/referrer) from the form page to the thank you page. So what I do:
I open my page with the form: example.com/index.php?variable=blabla
I get the variable and form the thankyou page URL
$urlconv = 'example.com/thankyou.php?variable=' . $_GET["variable"];
When the form is filled and submit is clicked the form redirects to the thank you page: header('Location: ' . $urlconv);
I even echo $urlconv on the first page to be sure that I've constructed the url correctly together with the variable. And it shows it correctly.
Unfortunatelly the redirect omits the variable for some reason. It goes only to example.com/thankyou.php?variable= for some reason...
Maybe by the time I call the redirect the variable is gone, so my question is can I somehow hardcode the variable in $urlconv? Because if the echo is showing it right and then the riderect omits it it means it has saved only a shortcut to the variable and not the actual value in the string, right?
I have very basic programming skills.
Thanks!
Try using the urlencode function. Along with that, if its within your browser, kindly omit the example.com and try using a relative link.
So
$urlconv = '/thankyou.php?variable='.urlencode($_GET["variable"]);
I've tried this right now, and it did work.
$urlconv = '/start.php?variable='.$_GET["variable"];
header('Location: '.$urlconv);
2nd solution: Try using javascript redirection. Also use double quotes while assigning $urlconv, as in $urlconv = "/thankyou.php?variable=".urlencode($_GET["variable"]);
<?php
echo "<script>location.href='".$urlconv."';</script>";
?>

Remove string from the link href

I'm using a jQuery pagination script and I'm using the onChange function so if a user click on the page number it does redirect him to the $_SERVER['REQUEST_URI'] + it adds an page number to the request url, but if I will click on some pages several times then the request url looks like this: &page=3&page=1&page=10 ... etc.
The code looks like this:
onChange : function(page){
window.location = '" . $_SERVER['REQUEST_URI'] . "&page='+page;
}
Now I need to remove $page=??? from the url if it already exist.
After this
$url = $_SERVER['REQUEST_URI'];
$url = preg_replace_all("/\\&page=[^\\&]+/", "", $url);
$url will contain the url barring the page attribute
The reason for this is that every time the user clicks on your link, the value of $_SERVER['REQUEST_URI'] is the current URL and you are just appending an extra string to the end.
You need to set the get variable to the page you want then just change this variable when your function is called. Something like:
$_GET["page"] = page;

How can I add GET variables to the end of the current page url using a form with php?

I have some database information that is being shown on a page.
I am using a pagination class that uses the $_GET['page'] variable in the url. When you click on a different pagination anchor tag, it changes $_GET['page'] to a new number in the url and shows the corresponding results.
I have sort and search features which uses the $_GET['searchby'] and $_GET['search_input'] variables. The user enters their search criteria on a form that is using GET. The variables are then put into the url allowing for the correct results to be shown.
The problem I am having is that whenever I click on a pagination link, it adds the $_GET['page'] variable to end of the url and erases $_GET['searchby'] or $_GET['search_input']. When I submit the search form, it adds $_GET['searchby'] and $_GET['search_input'] but erases $_GET['page'].
How can I add GET variables to the end of the current page url using the anchor tag and search/sort form without having it erase any existing GET variables, but overriding them if they're the same GET variable name?
Try this:
if (strpos($_SERVER['REQUEST_URI'], '?') !== false)
{
$url = $_SERVER['REQUEST_URI'] . '&var=value';
}
else
{
$url = $_SERVER['REQUEST_URI'] . '?var=value';
}
Go
Note that $_SERVER['REQUEST_URI'] gives you current url including query string value.
$query_string = http_build_query(array_merge($_GET, array('page' => $page)));

Categories