how to make back a href but not use history back
i have try to write this
config.php
$config['base_url'] = 'http://'.$_SERVER['HTTP_HOST'].'/webapp/';
and at index.php
( i have no idea to replace :
else{ die("wrong password back");
the final goal is
on index.php is form login
than if it's ok then next page.
if wrong then i want to create link back to http://localhost/webapp
not http://localhost/webapp/index.php or other history url
This is what you want:
die("wrong password <a href=\""
. substr( $_SERVER['HTTP_REFERER'], 0, strrpos( $_SERVER['HTTP_REFERER'], '/' ) )
. "\">back</a>");
Edit:
This will send you back to http://localhost/webapp not http://localhost/webapp/index.php
die("wrong password back");
There are some options to choose from, each has a downside
Base URL
You can use the base URL defined in config.php
die('wrong password back'); // repalce `"` with `'` to remove `\`; this a preference
This seems like the best option, however, the browser with request $config['base_url'] again. Unlike, back option which will go a step back (and leave all fields (inputs) as they were before submitting)
History API
else{ die('wrong password back');
This may and may not work, based on some condition, for example, having a set of previous pages in the history.
Related
How can i make it so if a visitor comes in from google through this old url
https://www.oceanreef.dk/shop/83-fisk-sundhed/8863-esha-exit---20ml/
go to:
https://www.oceanreef.dk/shop/83-fisk-sundhed/8863-esha-exit-20ml/
the problem is I made a new webshop for the client, and in the old shop he could make severeal hyphens. And now the url has only one ☺️
Can I fix it in htaccess or with a function?
thanks in advance :)
Didnt try anything myself yet
I would use something like the following in your header.php theme file...
<?php
if(is_404()) {
$request = parse_url($_SERVER['REQUEST_URI']);
$path = $request["path"];
if(strpos($path,'---')){
$result = trim(str_replace('---', '-', $path));
header('HTTP/1.1 301 Moved Permanently');
header('Location: '.$result.'');
}
}
?>
Place it at the top of the theme file before any other content gets rendered, otherwise the headers already get assigned.
The conditional checks if you are getting a 404 error page (because the old url page does not exist in the new website) and then it reads the path used. It will grab the old url as you shared, replace the extra hyphens with a single one, and then update the headers to take the user to the new location.
I would like to run one of JS scripts while redirecting to another webpage .. Script to change a variable in Index.php like below code:
if ($flag == 1) {
// echo "<h1> Welcome to Website Home Page </h1>";
if(header("Location: ../../index2.php")) {
echo "<script type='text/javascript'> document.getElementById(body).style.color = 'red'; </script>";
};
} else {
// echo "<h1>Try Again!!!</h1>";
echo "<div class='alert alert-danger' role='alert'>
Your are not a Registered User <i class='fas fa-exclamation-triangle'></i> <br> Please Use below registrarion form.</div>";
}
N.B:
Still Junior learning PHP.
An HTTP redirect tells the browser that it should get the data it asked for from a different URL.
The body of a redirect response should be a message telling the user that they need to request a different URL. It is used in clients which either don't have HTTP redirect support or which have it disabled. Today such clients are practically non-existent.
If you want to run JS on the page being redirected to then you need to embed that JS in that page.
The header function not is for check URL, this function for send HTTP header.
For check url path using REQUEST_URI in superglobal variable $_SERVER.
REQUEST_URI - the URI which was given in order to access this page; for instance, '/index.html'.
More: https://www.php.net/manual/en/reserved.variables.php
Run the following code for research:
echo '<pre>';
var_dump($_SERVER)
About your JS code:
You forgot to enclose the body in quotation marks
I think it will help you.
Add a slash before all single quote ' -- > \'
But above code dose not look ok...
You should add an event Listener, onclick, on something(when you check the URL) and then include your js in a function.
Is it possible in PHP to know which page a user came to a particular page from?
For example, the first page is index.php and the second page is index2.php.
Now the user goes to index.php from index2.php using a hyperlink which is on index2.php.
Now I want to store the link in a database, for the page the user came to index.php from, in this case index2.php.
If it's on your own site you might as well use the session, HTTP_REFERER is prone to spam or very likely simply not set, you can't trust it.
Maybe do something like:
<?php
session_start();
if (!isset($_SESSION['last_page'])) {
// first visit (landing)
} else {
// not first page
}
// insert into db
// set tracking for next page
$_SESSION['last_page'] = [
'page' => $_SERVER['REQUEST_URI'],
'time' => time() // know how long user was on the last page
];
Place it somewhere its hit on each page.
You can access the last page URL (which in this case will be the origin) using $_SERVER['HTTP_REFERER'].
If you only want to track sites that are from you, like index.php and index2.php then you should use a session and set the value on each page, its more precise than the $_SERVER['HTTP_REFERER'] variable.
$_SERVER['HTTP_REFERER'] can easily be spoofed be the user.
You can use the global variable $_SERVER, it should tell you where the user came from.
You can use it like this in your index.php:
echo $_SERVER["HTTP_REFERER"];
i'm trying to back with the javascript:window.history.go(-1); and add the ?error=1 on sequence in header(Location), its like
header('Location: javascript:window.history.go(-1);?error=1');
but it doesn't work, its possible back and add something ahead?
No, that's not possible. javascript: URLs are not valid in Location headers, and you cannot mix in the ?error=1 that way.
Assuming that this is actually in PHP, not Javascript, a more appropriate way to do this would be to determine the URL of the previous page from the referer header, and work from there:
$referer = $_SERVER["HTTP_REFERER"];
header("Location: $referer?error=1");
header() is a PHP function, that you are trying to mix up with some javascript. I assume that you want to do this in Javascript...
You can use document.referrer to get the previous url, which works only if the user arrived on the page by clicking on a link though: https://developer.mozilla.org/en-US/docs/Web/API/document.referrer
Then use: document.location.href:
document.location.href = document.referrer + '?error=1'
If you want to use PHP you need to use the $_SERVER["HTTP_REFERER"] variable which contains the last visited URL:
header("Location: " . $_SERVER["HTTP_REFERER"] . "?error=1");
There is a submit button that serializes some forms and submits them correspondingly to different handlers on the server. When the data have been sent, I would like to redirect to another page where the user can see an overview of the results. How can I do it so? Redirecting right away does not work since the page does not have enough time to submit the data (or it looks so).
The following fails:
$(document).ready(function(){
$('#submit_all_button').submit(function(){
var querystring = $('#q1').serialize();
$.get('/change_q1', querystring);
var querystring = $('#q2').serialize();
$.get('/change_q2', querystring);
var querystring = $('#q3').serialize();
$.get('/change_q3', querystring);
window.location.href = "/main";
return false;
});
});
The same problem arises when the submit action is to redirect to /main page.
Use the success function of jQuery get() and keep track whether all your updates are send. So the last success function call finally redirects your page.
I have an other answer in PHP which is interesting: run it as a background job (Other answer is the best way to go but if, for some reason, you can't use it):
set_time_limit(900);
ignore_user_abort(true);
header("Connection: close");
header("Content-Length: " . mb_strlen('ok'));
echo 'ok';
flush();
// Your code follows here
Maybe you can use this logic in your server-side language.