Using variables in header script - php

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='')

Related

How to get URL parameter and redirect after

Please I need help with how to write php code to get a URL parameter from and redirect to another page after. My URL looks something like this http://mywebsite.com/login.php?referrer=forum
The parameter I am interested in is the referrer=forum
I need this because i am trying to integrate a forum into my website which i am almost done with except for the single sign on(SSO) feature which allows the forum to use the register and login system i have already created for my website.
Thanks.
Something like this:
<?php
// get parameter from URL params
$referrer = $_GET['referrer'];
// redirect to another URL, including the referrer above
header('Location: http://someotherwebsite.com/?referrer=' . $referrer);
?>
You can store that parameter value in variable and pass in the link or you can use Session to use that variable across the multiple page.
<?php
session_start();
$_SESSION['referrer'] = $_GET['referrer'];
?>
And then in second page just use it from the session
<?php
session_start();
echo $_SESSION['referrer']; // use it as per your requirement
?>

PHP append variable to url in address bar

I would like to know if its possible to append a variable to url in address bar when page loads.
demo: http://communityimpact.com/discussions/
Current page is geolocated, if category = Central Austin...would like the url to load as http://communityimpact.com/discussions/central-austin
<?php echo $my_category; ?> = Central Austin
<?php echo $my_category_slug; ?> = central-austin
Any suggestions?
Actually headers are your best bet if you need to append variables to url.
Example:
$my_category = "Central Austin";
$my_category_grub = "central-austin";
header("location: community-impact/discussions/$my_category_grub");
//or append a var like this:
header(" location: community-impact/discussions/?location=$my_category_grub");
//then you could handle the request on the new page
But both of these methods would redirect you.
If you want to use $_SESSION vars would make more sense. I don't see any possibility of appending vars to URL without redirecting. Url is the address, if you change the Url then you HAVE to change page. But session vars live until they leave your site. And can hold that info hope this helps, I'm not sure what your purpose is.
$_SESSION["category"] = "central-austin";
But you have to have
session_start();
at the beginning of the page.
Try to use a redirection with headers, like
header("Location: http://communityimpact.com/discussions/central-austin");
Your probably should use JavaScript, there are two options here:
With a redirection
// similar behavior as an HTTP redirect
window.location.replace("http://communityimpact.com/discussions/central-austin");
// similar behavior as clicking on a link
window.location.href = "http://communityimpact.com/discussions/central-austin";
Without redirection

How to pass variables in URL and page?

My code in index.php:
<?php
$keyword = ($_GET['keyword']);
$adid = $_GET['adid'];
header("Location: http://www.tracking.com/base.php?asdf&keyword=&ad=" .$keyword .$adid);
?>
The tracking link is supposed to end with:
&keyword={keyword}&ad={AdId}
I need to pass the keyword and AdId through the URL onto the landing page which then redirects and pass both variables to the tracking link.
This is the URL I was trying:
www.example.com/?keyword={keyword}&ad={AdId}
I don't think I can test if my format above is correct unless I run search traffic to it.
I need help on getting the codes right and getting the URL to pass them correctly.
Edit: Did I format the URL to pass the variables correctly?
change this
header("Location: http://www.tracking.com/base.php?asdf&keyword=&ad=" .$keyword .$adid);
to
header("Location: http://www.tracking.com/base.php?keyword=$keyword&ad=$adid");
Try this:
header("Location:",'http://www.example.com/?&keyword='.$keyword.'&ad='.$adid);
try this..
<?php
$keyword = ($_GET['keyword']);
$adid = $_GET['adid'];
header("Location: http://www.tracking.com/base.php?keyword=$keyword&ad=$adid");
?>
You can try:
header("Location: http://www.tracing.com/base.php?asdf&keyword=$keyword&ad=$aid");
It seems that I post later,Ah.

Hide ?ref string in URL but pass it to script

How can I hide ?ref string from users but pass it to php code so it will grab it?
For example, visitor visits this page:
http://mysite.com/?ref=ref+string
In browser URL I want to hide it, so user will see"
http://mysite.com/
But I want to grab content of ref string via this:
$ref = $_GET['ref'];
Is it possible?
No, if you want to use GET variables, they will always be in the url.
However, you can use an alternative, like $_SESSION or $_POST variables.
You could try something like this at the top of your PHP script:
session_start();
if (isset($_GET['ref'])) {
$_SESSION['ref'] = $_GET['ref'];
header('Location: /');
exit();
}
You would have to use $_SESSION['ref'] to access the value from then on, however.
This is not how the http protocol works with query strings. If you have information that needs to be in the query string that you want to hide or obfuscate from the user, I would recommend a simple encryption to change it. If you're attempting to get rid of this information for aesthetic reasons, you will need to pursue a different method of generating the header or storing the information (session/cookies/etc).

need help in particular php job, dont know how to do it

I want to do something but cant figure out how to do this (i m newbie in php)
suppose, i have a list of URL's which shows live with this preg_replace,
$html = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
my output is like
http://localhost/get.php?url=http://yahoo.com
its obvious that you can view that links at output page, now i want to hide them at front page and make them clickable and working
something like we can change links into variables and then call them by clicking and something works in backgroud which can perform same thing as we are clicking on the link at front page
ya it seems bit confusing :(
you could save the url into a $_SESSION vars and when some users click the link retrive the url from $_SESSION and redirect to it...
//page1 - parse, save link in session and print a call to page2
<?php
session_start();
$_SESSION['url'] = preg_replace('/\s(\w+:\/\/)(\S+)/', ' GO ', $html);
....
echo 'GO';
?>
//page2
<?php
session_start();
header('Location: '.$_SESSION['url']);
?>
If i undeerstood what you meant...
Obviously now I used $_SESSION['url'] as a single string, but you can use a multidimensional array intead...
UPDATE:
anyway is better if you use an array on script..
example: http://www.test.org/go.php?page=# (where # is a number)
<?php
$array=("http://www.google.com","http://stackoverfloc.com","ecc");
//you can add more contorl in if statement, like between etc...
if (is_numeric($_GET['page']) header('Location: '.$array[$_GET['page']]);
?>
I'm sorry, maybe I'm not understanding quite well, but, isn't best approch use an array for in side server and use another Get variable to do that?
for example ?link=yahoo
and then
find link in array of url?
BY the way, I'm using NoScript and reports me like a warning..
You build up links with looking like this:
yourdomain.com/redirector.ph?url=#
where # represents an identifier.
In redirector.php you check if you know that identifier and send the redirect HTTP Header:
header("Location: http://www.example.com/");
Important Note:
You may not send any data before sending the header and the code after sending it won't be executed.
Info
http://php.net/manual/en/function.header.php
EDIT
Using this header is not absolutely proper in regards to standard, but it's not too far off, as the response really is at another location.

Categories