Hiding get variables in url? - javascript

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

Related

What is standard practice when dealing with an HTML POST form? [duplicate]

When registration form submits using POST method and when you refresh the same page then it will prompt for resubmit or resend information. How we can prevent this?
Use post-redirect-get. In short:
You can use ajax. You keep the same php code that you put in another file, and you post the data via ajax.
You can use include a one-time random token in the form. The first time that token is submitted (with other data), an action is taken. Future submissions of the same token have no effect (or just show the same confirmation page), and submissions with blank or invalid tokens are rejected.
This also protects against cross-site request forgery.
Try this:
<?php
if (!empty($_POST)){
?>
<script type="text/javascript">
window.location = window.location.href;
</script>
<?php } ?>
I placed it into prevent_resend.php and then included it after the postdata processing was done.
// ... save data from $_POST to DB
include('prevent_resend.php');
// ... do some other stuff

How to pass javascript variables to php variables with yii

First: I KNOW this is a duplicate. I am creating this because none of the answers to all the other questions satisfy me. I have a very particular situation where I'm using yii and tryi I can't send it through ajax because the php and javascript are on the same page on the same page.
Basically what I'm asking is if you have a page that uses yii and chart js, and you need to render a page that requires two arguments from the clicked bar, which is represented by activeBars[0]:
<script>
canvas.onclick = function(event) {
var activeBars = getBarsAtEvent(event);
<?php $controller->functionThatRendersView($arg1 /**(activeBars[0].value*/,$arg2 /**(activeBars[0].label*/); ?>
}
I don't care if it will render automatically, that is another problem. I just need to get those arguments to the php.
Thanks.
Also, if it helps, I am passing those two values to javascript through php for loops:
labels: [<?php for ($i=1;$i<=$numberIssues;$i++) {
echo $i . ",";
}?>],
The problem with grabbing $i and putting it into the label argument is that I don't know which bar label is the clicked one, I need javascript to pass the clicked bar values back to php.
Explain to us again why you can't use ajax. You say "because the php and javascript are on the same page". That's not what ajax is - you need a different URL for the ajax request, and a separate PHP file or something to handle it.
Without ajax it's impossible for javascript to send information to PHP, because the PHP runs on the server before the javascript runs on the client. Unless of course you want to do a complete page refresh, which is slower and generally worse from the user perspective.
I found an answer to my question! I'm just doing this for anyone else who is stumbling:
To pass javasctipt variable var jsInteger = 5; to php you type (in javascript):
window.location.href = "yourPhpFile.php?phpInteger="+jsInteger;
You access the variable in php like so:
$phpInteger = $_GET['phpInteger'];
This will require a page refresh, and will redirect you to the php file.

How to use Location with javascript and something more

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

Redirect after submitting multiple forms

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.

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