How to use Location with javascript and something more - javascript

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");

Related

How can I reduce multiple hyphens to a single hyphen for inbound URLs?

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.

PHP: How to use Javascript inside PHP code

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.

back link but not use history

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.

Hiding get variables in url?

I want to hide get variables in url
I want this
"dem/?p=user"
instead of whatever i passed like
dem/?p=user&act=stored
dem/?p=user&act=changed
dem/?p=user&act=deleted
Mates please help for me.
Advance thanks.
If you want to hide GET variables or values you should not use GET. In GET methods the data will always be send as part of the url. If you want to 'hide' the data (or not show it in the URL) from the user you should use a POST method.
You may use any one of the following:
Use POST method instead of GET
Use Ajax, with either POST or GET method
Use encryption and decryption if you really wish to send secured way i.e dem/?p=ENCRYPTED_STRING. Here ENCRYPTED_STRING will have all the GET data
Obviously since you did not take the time to tell us how you page structure is and how you are storing your values I can only guess.
Server side
If you store the data on a post-redirect page you can store it in a $_SESSION
session_start();
if (isset($_SESSION['message']))
{
$_SESSION['message'] = "succes";
}
Then on your confirmation page
session_start();
echo $_SESSION['message'];
Client side
You can create hidden inputs and add these to your form, set your form to post and handle these $_POST values on your form action page.
var x = document.createElement("INPUT");
x.setAttribute("type", "hidden");
x.setAttribute("name", "message");
form.appendChild(x);
Then on your confirmation page
if (isset($_POST['message']))
{
echo $_POST['message'];
}
modify .htaccess file to make short URLs the way you want them to look like

How can I use XMLHttpRequest to grab the title of a page and return it to my page?

I got the basic understanding of AJAX down, but I am not sure if there is a way to just use it to read a DOM and send the information back to be used on a page...
In my specific case, links to news stories are being stored in my database, and I am trying to get the text between <a href> and </a> to be populated with the actual title of the story.
Any ideas? Thanks
First of all, you'll need a proxy on your own server, because cross-domain requests are usually not allowed. A simple proxy would just echo the page to you, but for efficiency, it really should use something like regular expressions to simply return the page's title, which is what you want. For example, in PHP:
$text = file_get_contents($_REQUEST['newspage']);
preg_match("/(?<=\<title\>)[^\>]+/", $text, $matches);
if(count($matches)) {
echo $matches[0];
} else {
echo "Unknown title";
}
It's easy to use - just send a simple GET Ajax request to the script with a newspage parameter, and put the result into the link.

Categories