change PHP session id on link click - php

I am trying to create an HTML link that changes the PHP session id. Basically I need to call the session_regenerate_id(); when a link is clicked. The problem is that it can't run on the page that I am linking to. There is an upload form that reloads after upload so the function cannot be on the page I am linking to.
Can I do an AJAX post to a separate PHP file?

In your php file called regenerate.php:
<?php session_regenerate_id(); ?>
And then with jquery.
$.POST('url/to/regenerate.php', function() {
// callback
});
Then to run it:
$(function() {
$('#postNew').click(function(e) {
e.preventDefault(); // stops the link from going anywhere
$.POST('url/to/regenerate.php', function() {
// callback
});
});
});

Related

call jquery click function from other page

Can i get variable if my button from other file..
example page 1 (a.php):
Button
and page 2 (b.php):
$(document).ready(function() {
$('a.php .mybutton').click(function() {
alert("ok");
});
});
If you put your function in an external js file then you can fire it from any page. then it wont be a click function but a regular js function. Refernce the js file at the bottom of any page where you want to use the function.

php ajax prevent default

A have following code:
if (isset($_SESSION['login'])) {
print("<a href=\"add-to-cart.php?uuid=". $Item->PartId ."&price=". $Item->Price."\" >Add to cart</a>");
}
I heard about Ajax(sending form without page reload, e.t.c)
So as you see I have link a href
to add-to-cart.php?uuid=someID&price=someprice
This code works, and PHP script adds to DataBase record, and navigates to this url. But I dont need to navigate to this url.
So How could I do this, executing PHP with these dynamics parameters and prevent navigating to this URL?
But how could I
You have to add a javascript click handler to the link so that users send AJAX-request instead of navigating to the URL.
Here's a snippet using jQuery (assuming that your links have lnk-ajax class):
<script>
$(function() {
// Send AJAX request instead of navigating to the link's URL
$('.lnk-ajax').click(function(e) {
var linkUrl = this.href;
// Send AJAX request
$.ajax(linkUrl).done(function(data) {
// process the result
// ... put your code here ...
alert('Success');
});
// Prevent default link behaviour
e.preventDefault();
});
});
</script>

PHP redirect and load content with jQuery / AJAX

I have to create a simple website with various pages (5-6). Now I have one "master" page called index.php like this:
<html>
<head></head>
<body>
<div id="content"></div>
</body>
</html>
This page has also other div's on it, like a header where I show some images etc. and is styled with a css.
Now when the user clicks on a link in the menu bar, whatever page I want to show is loaded via jQuery/AJAX into the content div. I do this here:
$(document).ready(function () {
// Per default load home.php
var url= "home.php";
$.ajax({
url: url,
success: function(data){
$('#content').html(data);
}
});
$('#register').click(function() {
var url= "register.php";
$.ajax({
url: url,
success: function(data){
$('#content').html(data);
}
});
});
// more pages here..
});
This works perfectly fine and I am happy with the result, now on to the problem.
On some of these pages (like register.php) I have forms that I submit. Now once I submit the form and do various things like database operations etc. I want to redirect to one of the other pages and maybe show a short information message. What I do to redirect in the php file is the following:
header('Location: ../app/index.php');
I got this somewhere from a code snippet, so I am not sure if this is the proper way to do this. Because I have per default set the index.php to load my home.php content, it always redirects to my home.php content and I was happy with this. But what if I want to redirect to another page, lets say go.php? How would I do this? Is this even possible from php or do I need to do this with javascript/jQuery?
I am a bit lost here, have tried searching for it but didn't come across exactly this issue here.
For submit form and database connection :
you can use onclick function to handle actions for submit button :
example in .JS file
$(document).ready(function(){
$('#submit').click(function(){
var input = $('#textfiel').val();
$.post("ajaxHandle.php",
{data:input }
).success(function(responce){
$('#responceMessage').prepend(responce);
/* This is the field on your index.php page where you want to display msg */
});
});
});
ajaxHandle.php file:
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$inputData = $_POST['data'];
/* your database code will be done here you can find user input data in $inputData */
echo "registration successful"; /* this string will be get in var responce in .JS file */
}
To redirect page dynamically :
you can use .load() function for example :
$('#content').load('../app/register.php');
use this http://api.jquery.com/load/ reference to use it.

Best way to do a php action when a link is clicked?

I've got a database including uploaded documents. The link to an uploaded document is:
<a href="http://<?php echo $_SERVER['HTTP_HOST'];
htmlout('/database/docs/'.$cand['id'].'/'.$docs['filename']);?>">
<img src="http://<?php echo $_SERVER['HTTP_HOST'];?>/database/img/cv.png" border="0">
</a>
I want to call a function which writes to an activity log when a link like that is clicked, e.g. writelog($userid, $msg) so I can see what user has viewed what document.
What would be the easiest way to do that?
I would add a js/jquery click event on the image and have that make an ajax call to your php writelog function. Whether this is the best method is up for debate but that is how I would accomplish the task.
<a class="clicked_link">
$(".clicked_link").click(function() {
var file_name = .....
$.ajax({
...
})
});
Here is a non-js version:
Have the link load a php page that loads the image and then every time that page is called have it display the image and call the writelog function
<a href="load_image.php?filename=PHPCODE">
<php
$file = $_GET['filename'];
writelog(one, two);
You should use ajax function to send request to PHP Script when user click on anchor i.e.
Note: This code requires jQuery
$(function(){
$('a').click(function(e){
e.preventDefault();
$.post('<URL_TO_PHP_FILE>', { data: { userid: 1, message: 'clicked' }, function(){
alert('Log has been written');
} });
});
});
In PHP, you can write your code to save log.

Jquery delay link redirection for ajax

When a link is clicked, I want to clear the php session before redirecting to the destination page.
The way I thought of doing it is by loading a php script that clears the session with ajax in the origin page and once that script has loaded, to redirect to the destination page.
Here is the code I have:
$(document).ready(function() {
$("a").click(function(){
var clearSession = "clearsession.php";
$("#content").load(clearSession,"");
});
});
<a href="destination.html">
Currently it seems to follow the link before the php script completes loading.
A few guidelines :
You can't make the link point to any other page than "destination.html"
You can't add variables to the destination page either (destination.html?clear)
Use the load callback so you can execute the redirection after you receive a response.
$(document).ready(function() {
$("a").click(function(){
var clearSession = "clearsession.php";
$("#content").load(clearSession,"",function(){
window.location = $(this).attr("href");
return false;
});
});
});
<a href="destination.html">
Why not just redirect to clearsession.php?link=destination.html, which clears the session and then re-redirects to the desired URL?
$(document).ready(function() {
$("a").click(function(){
var clearSession = "clearsession.php";
$("#content").load(clearSession,"");
return false;
});
});
With "return false;", the browser won't follow the link.

Categories