Using $_GET with brackers in URL - php

I have the following code...
<?php
if ($_GET['custom_fields%5Bcartredirectmb%5D'] == 'test')
{ header("Location: http://www.google.com"); exit(); }
?>
I just need this code to get the parameter 'custom_fields[cartredirectmb]' and redirect based on its value.
It looks like it's hung up on the brackets in the parameter. I've tried using urlencode, but I'm not getting anywhere. I assume there's a simple answer to this get working.
Feedback? Thanks!

PHP automatically expands array expressions in the URL into the $_GET variable.
The following variable in the URL,
?custom_fields[cartredirectmb]=test
can be accessed through the cartredirectmb key, in the custom_fields array in the URL.
$_GET['custom_fields']['cartredirectmb']

Related

Using variables in header script

I want to pass some information to another page using "header" but I also want to use a variable for the location, for example:
With a static URL in location:
header("Location: mypage.com?fname=$_POST['fname']&lname=$_POST['lname']");
I want to use a variable like so:
header("Location: $myVariable?fname=$_POST['fname']&lname=$_POST['lname']");
But I can't for the life of me get the syntax right, as shown above it will throw an error, any help is appreciated.
Tom
You need to set a full url (with http://) and use put arrays between parentheses {}.
Working well with a static url:
header("Location: http://mypage.com?fname={$_POST['fname']}&lname={$_POST['lname']}");
Working well with a url variable:
$url = 'http://mypage.com';
header("Location: $url?fname={$_POST['fname']}&lname={$_POST['lname']}");
I hope this helps!
There are two way you can achive this, one is use session and another this url strucutre,
In session :
session_start(); // this should be on top of login_check file
// this goes just before redirect line
$_SESSION['fname'] = $_POST['fname'];
Or use url structure as follows,
header(wwww.example.com?action='')

$_GET reads url as string and not as an array

My $_GET is reading the URL as a string and does not separate different GET variables.
My URL is:
www.mysite.com/index.php?cat=archive?page=2
Using $_GET I'd like to use the variables $cat and $page.
echo $_GET['cat'];
Returns: archive?page=2.
echo $_GET['page'];
Returns: nothing.
What am I doing wrong here? Should I manually separate the URL into variables?
the '?' is only used once in the url. you should use & to separate each variable.
your URL should be www.mysite.com/index.php?cat=archive&page=2
and then you can echo on cat and page
url in not correct form where you are made it
www.mysite.com/index.php?cat=archive?page=2
correct it with
www.mysite.com/index.php?cat=archive&page=2

PHP passing a variable with header redirect

I am trying to pass a variable within a header link. I have tried below code
$name = "mike";
if($name != "lopan"){
header("Location:error-page.php?$errorMssg=Please enter information?");
}
This page redirect to location but doesn't pass the variable that contains the message. But when i create a simple link with values like this:
link
it passes it just fine.
Any ideas what i am doing wrong? or i can not pass information with headers?
You need to use urlencode like this:
if($name != "lopan"){
header("Location:error-page.php?errorMssg=".urlencode("Waoo so your not EVEN going to try to enter information huh?"));
}
And in error-page.php, you should get it (no need to urldecode):
<?php
$errorMssg = $_GET['errorMssg'];
Remove the $ before errorMssg and urlencode the message.
could it be because you have $errorMssg instead of $errorMsg ? also try to make a valid url, for example replace " " with %20 etc, function urlencode() could help you with this.

Display URL variables in webpage using PHP

I have data being passed via HTTP post to another page. Essentially passing data from one server to another. On the target page, I cannot get the URL variable to be seen by php. Am I doning something wrong? Is there a better way to do this?
URL string:
form_listener.php?
contactId=101460&inf_custom_ddState=IN&inf_custom_txtZipCode=46268&inf_custom_ddClientDegreeId=729&inf_custom_txtCity=indianapolis&inf_custom_txtLastName=Anderson&inf_form_xid=f28acf3ff321cb273cb4696e996008e0&inf_custom_ddStartSemesterYear=Fall2012&inf_custom_ddMilitaryAffiliation=Yes&infusionsoft_version=1.23.11.30&inf_custom_txtFirstName=someone&inf_custom_txtAddress2=&inf_custom_txtAddress1=4707+East+72nd+Street&inf_custom_ddHSGradYearCustomLiberty=2011&inf_form_name=LibertyOnline&inf_option_Signmeupforthenewsletter=432&inf_custom_txtEmailAddress=killing.fields%40gmail.com&inf_custom_affiliateid=D80576&inf_custom_ddEducationLevel=CLGJ&captcha.typed=jydqb
PHP Code:
$ddState= $_GET['inf_custom_ddState'];
echo $_GET['ddState'];
?>
You don't have ddState in the URI. You want: echo $ddState; (because that is the variable where you copied the data to) or rather (to avoid opening up an XSS security hole) you actually want:
echo htmlspecialchars($ddState);
When you use $_GET you are telling php that the variable is from the URL.
By doing this:
$ddState = $_GET['inf_custom_ddState'];
you are "creating" a local variable ($ddState) with the content of $_GET['inf_custom_ddState'], so you don't have to use $_GET variable anymore.
So your echo can be in 2 ways:
echo $_GET['inf_custom_ddState'];
echo $ddState;
$ddState= $_GET['inf_custom_ddState'];
echo $ddState;
// or
echo $_GET['inf_custom_ddState'];
you can use either this way
echo $_GET['inf_custom_ddState'];
Or
$ddstate=$_GET['inf_custom_ddState'];
echo $ddstate;
Your echo should just be echo $ddState;
You are assigning the value of the URL variable to a local variable. Once that's done, you are dealing with something that is locally scoped.
As the $_GET and $_POST are global array, you can use the following code to see the values:
echo "<pre>Get data</pre>";
print_r($_GET);
echo "<pre>Post data</pre>";
print_r($_POST);
Check whether these variables are set in the request. If not, try to use the post method, as it can hold more data than get method.
First of all, make sure that your page is getting request parameters. Try $_REQUEST['inf_custom_ddState'] or print all variables using print_r($_REQUEST).
$_REQUEST is an associative array that by default contains the contents of $_GET, $_POST and $_COOKIE. If anyting is passing to page then it should get printed.

How to display URL parameters in PHP script?

I want to redirect my browser to a PHP page such that when the page loads, it will display to the user a substring of the current URL.
For example, let's say I have a page called substring.php.
My browser forwards me to:
http://www.example.com/substring.php?oauth_token=123456
Is it possible to write some PHP code that will then display to the user, "123456"?
If so, can anyone help me on how to do this?
Thanks!
All the query parameters in the URL will be inside the superglobal $_GET array, so you could simply do this:
echo $_GET['oauth_token'];
BE forewarned that if you're going to output anything that comes in from a URL (ie. user input), you should make sure to sanitize it properly for output. In this case, htmlspecialchars() would be prudent:
echo htmlspecialchars($_GET['oauth_token']);
<?php
echo $_GET['oauth_token'];
?>
Can't you just use the $_GET superglobal? It stores the contents of the query string part of the URI as an associative array:
echo $_GET['oauth_token'];
You can retrieve the value of oauth_token via the $_GET superglobal array:
echo $_GET['oauth_token'];
Of course you should use caution when outputting data you get as input from a user, but that's how it works in short.

Categories