I am developing an app using twitter API,in which any user can enter his twitter username in textbox and get his followers. On home.php , I am using jquery. on click event of button, I am calling getinfo.php and passing textbox value along with it. This is the case so far.
What I want to do?
Right now my url is:
http://www.example.com this redirects to
http://www.example.com/home.php
Now I wish that my URL should also work like this:
http://www.example.com/username
means when user will type sitename and his username he should get his results.
As I am trying jquery first time, I am not getting how can I pass value of URL (username in this case) to getinfo.php directly?
or any other solution?
Can you guys help me with this? please ask for any details.
Your help will be greatly appreciated.
Thank you in advance.
Here is a posted code on demand
the click event in jquery:
$(document).ready(function(){
$("#btonsubmit").bind('click', function(){
$.get('/getinfo.php?tnm='+$('#tnm').val(), '', function(data){
$('#get_data').html(data);
});
}
)};
// here tnm is name of textbox. and btonsubmit for button
You can do this like below.
step 1: update your .htaccess file . add one line
ErrorDocument 404 /notfound.php
This will redirect your user to /notfound.php
step 2:
on notfound.php,
get any how URL that user has called like http://yoursite.com/username
below line might help you to get that URL, not sure...
$url = $_SERVER['HTTP_REFERRER'];
extract username from this url.
check whether this userName exist?
if exist process getInfo.php with that
username..
else redirect to notfound.html..
I am not sure as this will work or not?? the main Question is whether you will get URL with $_SERVER['HTTP_REFERRER'];
Related
I have this kind of a problem I can't get over with. So I have a page where you can update the information for a specific user in mySQL database, so the URL would be like this:
...updateemployee.php?userid=4
where the userid is the unique id of an each row from the database. And then I have a button on the same page that opens up a modal where I have an input that updates the password for that specific user which uses another .php file (changeemployeepwd.php) to do this. Everything works perfectly fine however let's say the input is empty meaning that I want to redirect the user back to this page: ...updateemployee.php?userid=4 and add a parameter in the URL that would open up a SweetAlert window saying that the input is empty, therefore the password can't be updated, so for this I wrote this code in (changeemployeepwd.php):
if (empty($_POST['newpwd'])) {
header('location: updateemployee.php?userid='.$userid.'?empty');
exit;
}
So when the (changeemployeepwd.php) will redirect the user back to ...updateemployee.php?userid=4 the full URL would be this: ...updateemployee.php?userid=4?empty
By the way I have the update password redirect done the same way:
If ...
header("location: updateemployee.php?userid=".$userid."?updated");
Okay now the problem is that if the URL stays as ...updateemployee.php?userid=4?empty and this time I put some value in the input it goes again to (changeemployeepwd.php) and as intended it should change the password but instead it just adds ?updated to the URL and doesn't change the password. so the whole link looks like this afterwards:
...updateemployee.php?userid=4?empty?updated
I thought that the problem was in this part:
".$userid."
Therefore, I tried to use either:
header('Location:' . $_SERVER['HTTP_REFERER'] . '?empty');
or:
header('location:javascript://history.go(-1)' . '?empty');
But none of them are working and I'm not sure what else to use here so if you have any ideas or solutions it would be greatly appreciated. Thanks in advance.
EDIT
In updateemployee.php I've done the form action like this:
<form action="changeemployeepwd.php?userid=<?php echo $_GET['userid'] ?>" method="post">
I have a simple PHP website. Let's call it youtubex.com. I want to redirect youtubex URLs (in the format shown on STEP2) to my website in the format shown on STEP3. Here, I am using YouTube, just for illustration.
STEP1: https://www.youtube.com/watch?v=lj62iuaKAhU
STEP2: https://www.youtubex.com/watch?v=lj62iuaKAhU
STEP3: https://www.youtubex.com/#url=https://www.youtube.com/watch?v=lj62iuaKAhU
STEP1 shows any desired URL. STEP2 shows the same URL from STEP1 with youtubex as domain. STEP3 shows the final required URL. I am trying to redirect STEP2 to STEP3.
I tried finding some solutions to this on the internet and SO, but, none help. Here is one.
This should do the work:
RedirectMatch 301 /watch$ https://www.youtubex.com/#url=https://www.youtube.com/watch
An inefficient but full php solution can be using the location header in php :
$vid = $_GET['v']
if($vid){ header("location:https://www.youtubex.com/#url=https://www.youtube.com/watch?v=$vid");
}
by the way you can't get the text after the hash mark in php because it is not sent to the server.
javascript can do it in a more neat way without the second reload by checking window.location.href to see if the hash does not exist already and then get the v parameter in url then change url without refreshing the page by using window.history.pushState({"html":response.html,"pageTitle":response.pageTitle},"", urlPath);
using php str_replace and header :
$step2_url = "https://www.youtubex.com/watch?v=lj62iuaKAhU";
$part2_url = str_replace("youtubex","youtube",$step2_url);//the output is : https://www.youtube.com/watch?v=lj62iuaKAhU
$step3_url = "https://www.youtubex.com/#url=".$part2_url; //the output is : https://www.youtubex.com/#url=https://www.youtube.com/watch?v=lj62iuaKAhU
now you have the final url , simply redirect
header($step3_url);
My current url is http://domain.com/example.php/link=eg But if someone plays with url and puts url as http://domain.com/example.php/abcdefgh97654 - anything, my all functions all links from that page become inactive.
I tried using <?=HTTP_SERVER;?> before all links in my php files but as my website has user registration and sign in, when user signs in and clicks on any menu (link from php script). It throws on index.php page. But if user logs in again it works perfectly. In short user needs to log in twice to work everything perfect.
Basically I want two solutions one I want to redirect www dot
domain dot com/example dot php/abcdefgh97654 - anything (wrong url
page) to errorpage (I already done this in htaccess) but does not
work for above type.
List item
And want to solve two time log in problem.
If anyone has solution for this will be appreciated.
For you to do this, you have to know what values are supposed to be passed via $_GET variable to your page. Then you can create filter for the values and user header(); function.
Here is my suggestion:
<?php
$var=$_GET['val']
//get expected length if you need.
if(strlen($var)>9 or strlen($var)) {
$redirect=true;
}
//if you know what you are expecting
$vals=array('val1', 'val2, 'val3');
if(!in_array($var, $vals)){
$redirect=true;
}
//continue or replace your filters in this manner and asign your value to $redirect
if($redirect==true) {
header("Location:url"); // do this before anything else is printed to the page or else it will say headers already sent.
}
?>
I have a website (www.website.com) and on the homepage is a form, where the user set up a city. After sending the form, I am trying to have the following URL address:
www.website.com/city-name
But I am lost a bit here, because I am not sure, how to set up the form (GET or POST is better for this need?), and .htaccess.
Could anyone help with this? Thank you
After the form is submitted you could redirect your users via PHP redirect, or am i missing something?
header('Location: http://www.website.com/city-name');
But be careful, this can only be used before you send any output!
PHP: header()
You could let the submit button run a javascript function.
In that javascript function, use
var cityName = document.getElementById("the textarea id").value
to get the value the user typed for the city name.
Then redirect to the right page:
window.location='http://www.website.com/' + cityName
I'm building a bookmarklet and I need to get the current URL of the webpage the user is on when they activate the bookmarklet.
I tried using
$current_url = $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
But that will just get me the URL of the server where the JS for the bookmarklet is hosted. Is there anyway of getting the URL straight from the address bar?
You can resolve the problem by defining your SITE URL (eg: define('SITE_URL', 'http://abc.com')) and concat the site url with $_SERVER['REQUEST_URI']
Eg: $cur_url = SITE_URL.$_SERVER['REQUEST_URI'];
Yes, simply pass location.href form the bookmarklet.
For example;
location.href="http://mywebsite.com/bookie.php?url="+encodeURIComponent(location.href);
Then on your server, you get the URL at $_GET['url']. Good luck.
Of course, that's just JS. You need to add javascript: scheme for it to work.
If by bookmarklet you mean http://en.wikipedia.org/wiki/Bookmarklet than I think this code will help:
javascript:alert(document.location.href);
To test it, select the code and drag it in your bookmark browser bar than click it.