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.
Related
I have an affiliate program and have a serious problem that I can figure out.
My affiliates have links like this...
http://example.net/?p=14&ref=delta88
Once the page loads it changes to...
http://example.net/?p=14
Which totally gets rid of the ref id. I need it to keep the whole URL in the bar in case they hit refresh. Because when you hit refresh it takes the affiliate out of the system and just let's people join without an affiliate.
The way my code works for the pages is this...
That URL goes to an index.php file. In that file it finds all the affiliates information. It then uses an include to show the page. So it's not pointing directly to the page. I need to use the include because I store about 27 pieces of data in strings and I can't put that information in a URL as queries and have it forward to that page.
I added that information because it may be because of the include that's causing it and that will help you better figure out a solution for me.
Use a SESSION, its like a variable that holds for each user, here is a tutorial but works like:
<?php
session_start();
if(isset($_GET["ref"]){
$_SESSION["ref"] = $_GET["ref"];
}
?>
Now, in any PHP that open the user, will have that variable set ( $_SESSION["ref"])
you can keep current url in variable , see below used actual_link to hold the current url data.
if($_GET){
$actual_link = "http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
//if you want to redirect page on same url just call header with $actual_url
header('Location: '. $actual_link);
}
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>";
?>
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).
e.g. i have page with url http://mysite.com?page=3&var=10 also there is form on page.
When form submitted there some actions in php but i need to remove this ?page=3&var=10 after form was submitted somehow is there way compatible with all browsers trough PHP without mod_rewrite?
This is an old topic, but just in case anyone else is searching for this in the future, you can use the javascript replaceState to change the history and browser bar label. A simple php function to do this:
function set_url( $url )
{
echo("<script>history.replaceState({},'','$url');</script>");
}
Then would simply call this function with the desired url (presumably dropping the post variables):
set_url("http://example.com");
A page reload or a back after calling another page will now have the new url location in the history.
I think that using POST may be a more elegant solution, but if you must use GET this is a work around.
If you're using action=index.php, then all values will be posted to index php, ?page=3&var=10 will be automatically removed.
If you want to post to the same page you can either use 'action=index.php?page=3&var=10' or action=<?php echo $_SERVER['PHP_SELF'] ?>
You can check at the beginning of the page if something submitted and then redirect to whatever you want with header('Location: http://www.example.com/'); More about header function http://php.net/manual/en/function.header.php
Yeah, the solution is quite simple (even if not really SEO friendly):
<?php
header("Location: http://mysite.com")
?>
just for information...why do you need it?
use parse_str to get the query string as an associative array that is easy to modify. Then use http_build_query to convert the associative array into a query string.
$queryString = $s['QUERY_STRING'];
$params = array();
parse_str($queryString, $params);
//change $params as needed
$queryString = http_build_query($params);
if ($queryString) {
$queryString = '?'.$queryString;
}
return preg_replace("/\\?.*/s","",$s['REQUEST_URI']).$queryString;
preg_replace("/\\?.*/s","",$s['REQUEST_URI']) removes the original query string allowing you to replace it.
Does this work for you?
header('Location:/');
mod_rewrite cannot affect what's displayed in the user's browser address bar, UNLESS the rewrite does an externally visible redirect. Otherwise it only rewriting things within the webserver, and that's invisible to the user.
If you want to affect the user's address bar, you'll have to do a redirect via header('Location: ...') after the form's finished processing.
I'm using a form to submit some post information to a PHP script. After the script finishes, I want it to redirect right back to the page the user came from. Right now I'm just using header() with a static URL. I've found a ton of very conflicting information about this around the internet, so I'm wondering what StackOverflow thinks.
Use HTTP_REFERER:
header('Location: ' . $_SERVER['HTTP_REFERER']);
access the $_SERVER['HTTP_REFERER'] variable and redirect to this. Should do the trick.
the way i would do it is use a session variable to store the current page URL everytime it is accessed.
$_SESSION['last_url'] = <get current url>
replace your static url in the header with $_SESSION['last_url']. Depending on how you implement your PHP, you can use search google for "current url php" or just $_SERVER['REQUEST_URI'] (stackoverflow doesnt allow me to put more than 1 link!)
Use REQUEST_URI But watch out for that presiding slash/