strip query string after page load - php

im developing php and my webpage urls are like this
http://localhost/myweb/?action=news
http://localhost/myweb/?action=my_profile
http://localhost/myweb/?action=my_upload
http://localhost/myweb/?action=my_collection
http://localhost/myweb/?action=chat
http://localhost/myweb/?action=my_friends
etc
and for example, in particular page there's delete feature that access a href link
http://localhost/myweb/?action=my_friends&delete=2414535435 <-friends id
i do this to make it tidy by making the link cant be seen without looking at the source
a href='#'
and using javascript to access the real link
$('.deleteclass').on('click', function () {
var name= $(this).attr('nameattr');
if(confirm('You sure want to delete '+name+'?')){
window.location='?action=my_friends&delete='+this.id;
}
});
the problem is, after i process the delete and load the page, i don't want the full link on the address bar.
http://localhost/myweb/?action=my_friends&delete=2414535435
i need to make it back look like this on the address bar
http://localhost/myweb/?action=my_friends
is it possible? through mod rewrite perhaps?

you can not strip a url after the browser has loaded the page.
Just do a redirect after your delete or other action.
header('Location: /myweb/?action=my_friends');
Add error/success messages to a session variable (in the action script before doing the header) so you can show them on the other page
$_SESSION['errors'] = "Delete Failed: For Some Reason";
And on the other pages check to see if the session variable exists, show it and then remove it (otherwise it will stay there and the pages will continue to think there was an error)
<?php
if(isset($_SESSION['errors'])) {
//Do some code to show the error
...
unset($_SESSION['errors']); //delete the error messages
}

Related

how to remove a parameter from url on page refresh php

I am sending error values in the url.For example if i have a website named
www.example.com
and the url for login page is
www.example.com/login.php.
If the user enters wrong credentials url will be
www.example.com/login.php?invalid.
So everytime i refresh url remains
www.example.com/login.php?invalid.
How to remove invalid from url on refresh???
I think that by using the invalid GET variable you try to determine whether or not to display the error message to the user. This isn't really a good way to do so, due to the number of reasons, one of which made you ask this question.
You have a number of options instead, one of which would be using the session variables to store the error message. E.g., if the user login fails, you could store the message in your session:
if (badLogin()) {
$_SESSION['errorMessage'] = "Something's wrong";
}
and then on the login.php page you could try and see if it exists:
// ...your HTML...
if (!empty($_SESSION['errorMessage'])) {
echo $_SESSION['errorMessage']; // show it to the user
unset($_SESSION['errorMessage']); // so as not to display it every time
}
// ...your HTML continues...
This is not the perfect way either, but without knowing your application structure it's hard to suggest anything else.

Redirect users to Long URL

I have an application where URL after rewriting are like this
http://www.domain.com/product/seller/product_id
an example link would be
http://storeiown.com/product/kitchenking/92013
This was okay but I need the title of the product to be included in the url
http://storeiown.com/product/a-very-nice-electric-cooker-by-kitchenking/92013
I achieved this too and all was good.
Now, I want all the url which do not include the title to redirect to this one.
Like, if user lands from the url without the title they should be redirected to a version of the page with the url containing the title in the url.
How do i accomplish that. And for info additional info I use CodeIgniter in the app, if that makes it any easier.
you can do this way which i use. In the previous page type this at the top:-
<?php
session_start();
$_SESSION['mainpass']= '0';
?>
And in the next page code this at the top of the page :-
<?php
session_start;
if(isset($_SESSION['mainpass'])) {
//run the current page
}else{
header("location: www.domain.com");
}
?>
If you are using codeigniter, you could try the below.
$seg3 = $this->uri->segment(3);
if(is_numeric($seg3)){
//The user has come without the header because the third segment is numeric thus probably using the product id.
//Therefore, redirect again to the proper link after getting the heading from your db
} else {
// do nothing
// the seg3 is not numeric means it probably came through normal preferred way
}
And in order to use the
$this->uri->segment(3);
You need to either
auto load the url helper
load manually when required

URL rewriting, same URL across multiple PHP files

Hey so I have create a login system to a website and I wish to have this login appear when I type in my address. When I have typed in details and logged in, I wish to be redirected to another PHP file, but with the same address.... this way, All I need to do is type in my address if I am allready logged in and I will go to the site which requires login.
I have made a transaction happen identifing if the session is created, if it is, it redirects me to another page, but also to another URL. I tried googleing it, but couldn't find anything exact and straight forward.
Currently:
Login page:
www.example.com
Member page:
www.example.com/members
What I wish for:
Login page:
www.example.com
Member page:
www.example.com
The program structure should look like this.
index.php
if (user is logged in)
display dashoard
else
display login page
Since you are using PHP, make use of session functions. Thus, URL rewriting is no longer necessary.
Update
Assuming if you have file structure in PHP like this:
- index.php
- login.php
+ template
- login.php
- dashboard.php
You can do the following structure in index.php file.
define('IN_FILE', true);
if (isset($_SESSION['user'])) {
require 'template/dashboard.php';
} else {
require 'template/login.php';
}
In template/dashboard.php
if (!defined('IN_FILE')) {
exit;
}
// Then your HTML, PHP and whatnot
And in login.php
if (!isset($_SESSION['user'])) {
require 'template/login.php';
} else {
header('Location: index.php');
}
Change the code according to your needs.
This can be achieved using several approaches.
a) Use session to determine the current page, so if a user click on a link, create a session store the value and on page load read the session data and include the file accordingly.
b) Use URL parameter to determine the page (this is the most common approach). for example in index.php you can add more parameters like index.php?page=somepage and by reading the value using $_GET and including the PHP file accordingly.
There are some more way to achieve what you want to, for instance using javascript/jQuery this is possible.

php link to 2 things

Is there any way in php to have a link so that when the user clicks on the link, they call a function and then immediately after calling the function, the user is redirected to another webpage.
$acceptEmailInvite; //append this url to the url in the link
if($app[UID]) {
//how do I have a link connect to two redirects?
$acceptEmailInvite = '/idx.php/store/accept/'.{$org['id']}; //link to this first
$acceptEmailInvite = 'getLink?org='.$D['bta']; //then link to this
}
Accept the invitation for: <b><?php echo $D['referer']; ?></b>
EDIT: I meant that these two things happen only when the link is clicked. So the user should not be redirected unless the link is clicked. Sorry for the confusion.
Of course.
<?php
foo();
header("Location: http://example.com/");
exit();
The simplest way to do this is to have a page that performs the action, then redirects the user. For example:
/idx.php/organization/accept/
<?php
// Get your ID's, presumably from the URL
// Accept the invitation
acceptinvite($org['id']);
// Use header to redirect the user when you are done
header("Location: getBuzzed?org=".$D['betacode']);
exit();
?>
Your link would look like this:
Accept the invitation for: <b><?php echo $D['referer']; ?></b>
.. but the user would see theirself go directly to getBuzzed....
You can see this functionality in action at Google or Facebook. If you google StackOverflow.com, the link in the results actually points here:
http://www.google.com/url?sa=t&rct=j&q=stackoverflow&source=web&cd=1&cad=rja&sqi=2&ved=0CCsQFjAA&url=http%3A%2F%2Fstackoverflow.com%2F&ei=ii-dUKaiMc_Psgad8oGYBA&usg=AFQjCNERidL9Hb6OvGW93_Y6MRj3aTdMVA
Google uses this page to track who has clicked on the link, and alert you if this link is dangerous.
Important
The page with the header command must not echo anything. If it shows any content, then you will get an error message when you try to use the header command.

Redirect website from within frame PHP

I have a web page, let's call it main.php which displays an image of football field and some players distributed on the field. However, that page uses list.php as a right side frame that loads a list of players.
What happens is, when the user clicks on a player on the field (main.php), let's say on the image of the Goal Keeper (GK), a list of GKs from world wide teams will load in right list fram (list.php). This is by using ajax.
So far we are good.
The current situation is, when session times out and the user clicks on a player from the field, the list on the right does not load, instead, list of players disappears from the list and a message says "Please login" is displayed on the right side frame (list.php)
The objective is, when session times out I want the whole website to redirect to the main page index.php
The problem is, I already put the redirecting code just before the code that is responsible of displaying the message "Please login". But what happened is, the redirection happens from within the frame, so i ended up having main.php displaying the field, and list.php displaying the main page!
Here's the code I added.
$user_id = NSession::get('user_id');
if (!isset($user_id))
{
NSession::removeall();
General::Redirect('index.php');
}
They are using Smarty. and btw, I added the same code to top of main.php, and now if user tries to access main.php without logging in, it will redirect him to the main page, so the code works!
n.b. The project is not mine, it belongs to the company I work in.
And I don't know which code is checking the session, all what I know is, if the user click on a player from the field after the session timeout, the "Please Login" message will be shown in the frame.
I'm guessing the redirect is essentially the same as using a header() function. It isn't possible to specify a target using a php redirect as it is server-side - specifying the target is client-side.
You would need to print something like this to the screen:
<script type="text/javascript">window.open('index.php','_parent');</script>
And that will redirect the user to the index.
Using frames for such purpose is... well... so 80ish...
Anyway, the frames are probably named in such a scenario. This means you can address them, but also that you have to address them. Just loading an url inside the "current" frame does exactly that, which is why your approach won't work.
If you really have to go with that frame based approach, then you will have to use javascript to address all known frames and redirect them.
Maybe you can use some javascript inside of your frame like so :
<script type="text/javascript">
window.top.location = 'YourPage.html';
</script>
Hope this helps
The issue was that the session expires while I'm on main.php. Therefore, any subsequent Ajax requested will fail since all requests requires session to be active.
the problem was that the Ajax request being sent from the IFrame (the IFrame is inside main.php and points to list.php thru Ajax calls) is failing due to session expiry.
So I've fixed this issue by adding another two session checks, one on main.php, list.php using PHP (check for session, if it's there, redirect). And in the main container, main.php, I check for the session via JS, interval Ajax requests to check the session, if session has ended, then use redirect using JS.
PHP:
$user_id = NSession::get('user_id');
if (isset($_POST["checklogin"]))//check loging
{
die(isset($user_id) ? "true" : "false");
}
if (!isset($user_id) || $user_id == "")
{
NSession::removeall();
General::Redirect('login.php');
}
JavaScript:
jQuery(document).ready(function($) {
$(window).focus(function() {
checkSession();
});
});
function checkSession()
{
$.ajax({
type: "POST",
data: {"checklogin": "cl"},
url: "list_players.php",
success: function(result) {
if (result === "false")
{
if (FIELD.showMessage === false)
{
FIELD.showMessage = true;
alert("Your session has been closed\nYou will be redirected to login page now. ");
window.location.href = ("login.php");//incase user clicks OK
}
}
}
});
}

Categories