htaccess and php generated links - php

I have a form that a user fills out, which submits information to my mysql database. On submission, my php code creates a url based on form data that was submitted and a unique ID. Rather than having a new webpage populate my web directory based off of the generated url, I would like to point that url to something similar to a "profile" page.
So my php creates a link after a user has submitted the form that looks like this: http://localhost/State=ALCity=SeattleEvent=eventSubject=titleID=135
This link gets stored inside my table along with the data that was submitted by the user. I've read that storing a url inside a table isn't practical, however for my purposes the table data will expire and get deleted within a certain amount of time.
In short, I'd like to be able to have a user click the link which will pull up data that was submitted. Can I grab data from the url to pull that information up for the user? Or will I need to change my setup that's already implemented? I can explain further if needed :)

If I get you right, this is a classic case for the mod_rewrite extension of Apache (since you mentioned .htaccess, I think you use Apache as webserver). With this mod, you can filter parts of the url and send it to your php script as normal $_GET parameters.
You will need something like this in your .htaccess (just out of my mind)
RewriteEngine on
RewriteRule ^State=([a-zA-Z0-9]+)=SeattleEvent=([a-zA-Z0-9]+)=titleID=([0-9]+)$ profile.php?state=$1&event=$2&id=$3
What this is actually doing, is that the Apache checks if the current requested url match the regular expression of this rule. If that is the case, it will call the profile.php and pass the "dynamic" part of the url as $_GET parameter (using the backreferences).
For further information, I recommence the official mod_rewrite documentation, the mod_rewrite guide and this nice tutorial.

Related

How to create new webpages automatically based on user input?

This question may have been asked before but I couldn;t really find it.
What I want to do is, websites like pastebin.com, even stackoverflow, these generate new webpages based on user input showing that data from what I can understand.
I want to make it like that. User enters something, and he is given a permalink to share that information.
How to do this using PHP ?
EDIT: Here is an example
Like, I want it that
instead of having something like www.example.com/view.php?id=123
I want it like
www.example.com/view/123
This is impossible to be done with PHP, you can do this with .htaccess's url rewriting.
a good article about this can be found at: https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
In this site , work some ajax function, when you add some data in textarea and click submit, called function , that saved current data in db, and return some json data , and then show you in some format.
Simply done like this:
Create a index.php containing a input field where the user inputs their data.
Post data to the server and create a random string which will be the users permalink.
Then insert the random string and data into a database.
In the index.php file say that if any uri is added after your domain like example.com/
it checks against the database if this code exists in db, and returns the result to the visitor.

auto-populate another form using a link, it is possible?

I'm trying to make an app on Android that send an URI that auto-populate the "RFC Emisor" and "RFC Receptor" of this web page:
https://verificacfdi.facturaelectronica.sat.gob.mx, if I'm correct those two inputs have the id of:
ctl00_MainContent_TxtRfcEmisor
ctl00_MainContent_TxtRfcReceptor
I already tried this but it didn't work:
https://verificacfdi.facturaelectronica.sat.gob.mx/&ctl00_MainContent_TxtRfcEmisor=123456789&ctl00_MainContent_TxtRfcReceptor=123456789
there is a way to achieve what I want?
The short answer is no. The browser won't automatically detect the URL parameter and pre-populate any form fields. A back-end PHP / ASP.NET page can read the value from the request and generate the HTML fields with the specified values. Alternatively, the page could use JavaScript to set the field values when the document finishes loading.
But all of this depends on changes to the target web page. If you do not have the ability to modify that page, I'm afraid there's very little you could do.
You might be able to duplicate the form on your own page, and send the form data to the target—effectively bypassing the form on the other page and 'faking' your own, but if the target system does some kind of validation to prevent posting forms across domain names, this probably won't work either. You may have create the form and process it yourself, replicating the entire form interaction programmatically when a user submits a form to your server. In any case, none of these options are particularly graceful.

How do I rewrite a POST request from a form to a user-friendly URL with htaccess?

As the topic - How do I rewrite a POST request from a form to a user-friendly URL with htaccess?
The scenario:
I have a webpage that uses a search-form. When I submit that form using method="post" it works flawlessly. BUT I don't get any text in the browser address-bar (of course), but that's exactly what I want! And that by using method="POST", NOT method="GET"!
Let's say I search for "banana". The PHP script translates the POST-request and the script shows all receipts with the word banana in it. But the URL then of course shows something like http://www.example.com/search/ (yes I use mod_rewrite for that). I want the URL to look like http://www.example.com/search/banana/.
The original request from the server looks like ....xample.com/index.php?p=search and the post is of course hidden and would otherwise be ....xample.com/index.php?p=search&q=banana.
I'm not new to mod_rewrite rules and conditions but I just can't get it to work...
Thanks in advance!
Quoted from: Apache mod_rewrite question
You can't use POST data for mod_rewrite. This is because the POST data isn't in the HEADER of the http request, it's in the BODY.
My suggestion would be that you perform an action on the posting page that adds the prefix to the URL, which would mean you don't even need to rewrite.
If I understand correctly: you want the browser to POST to http://www.example.com/search/banana/ rather than to http://www.example.com/search/, where banana is one of the form's input-fields. (Is that right?) That's not really a mod_rewrite issue, then, so much as an HTML issue: it happens on the client side. And since HTML doesn't support this, it's actually a JavaScript issue. You'd have to write either an on-submit-handler for your form, or an on-change-handler for the input field. In either case, the handler will have to modify the form's action based on the contents of the input-field.
(Note: The above is also true, to a certain extent, for GET requests. The major difference is that with a GET request, you could circumvent this by HTTP-redirecting from http://www.example.com/search/?q=banana to http://www.example.com/search/banana/; so the browser will initially GET http://www.example.com/search/?q=banana, and then GET http://www.example.com/search/banana/. But according to the HTTP spec, you can't redirect a POST request in the same way.)
You will need javascript.
I answered a similar question before. When someone click on the submit button you will need to change the action attribute at the form tag.

JavaScript Redirection Loop

I'm trying to implement a Facebook connect button to my website.
It's working, but it's written in JavaScript and I'd like to insert the information into the database (the tutorial I used is linked below.)
I'm trying to redirect the users from the index.php to index.php?name=xxx.
The name value is rendered with JavaScript so I can't simply just set a PHP variable, can I?
I've been told it's possible with $_GET so that's what I'm trying to achieve.
Using location.href/replace, both redirect to the same page over and over again...
This is the tutorial I used.
I would like to store some information in the database, but I can't do it directly since it's JavaScript and not PHP.
Is there a solution/any other way?
check if the name parameter is filled or not, and if it is, do not redirect

.htaccess converting id to user via mySQL database

I'm looking to use mod_rewrite to mask user profiles on my site. However, their profiles are decided by their id in the format /profile.php?id=1. Both 'user' and 'id' are in the mySQL table 'users'. Is there a way to reroute the URL to read /user? Sorry if that's badly explained!
RewriteEngine on
RewriteRule ^profile/([^/\.]+)/?$ /profile.php?user=$1 [L]
This will cause all /profile/username to be redirected to profile.php?user=username. You can then check in your PHP:
if (isset($_GET['user'])) {
// Check if the username exists.
// SELECT ... FROM users WHERE username = ..
}
.htaccess will not be able to use the info from the database to do what you desire. Even if it could you shouldn't do it :).
But what you can do is to change the profile.php to be able to read an user by the name. Let's say to make profile.php able to process a new parameter name= and you add logic in it to look in the database either via an id or via the name. If you have this then is actually trivial to route / to /profile.php?name=.
You will also have to change the website generation to output proper urls.
Also look at Cristian's answer for more technical details. It's the same idea in the end.
You wouldnt change this in htaccess, or with any kind of config/rewrite rule. This would be done in your application PHP if I understand what you want.
in /profile.php?id=1 profile.php is (rewrite rules aside) the source PHP file, and id is a GET parameter. Its like an argument to profile.php when it executed. They come in the form source.php?get1=val1?get2=val2.... and you access them through the array $_GET
If you don't want users to see that id, you can do one of two things.
Replace it with user, as you suggest. URL would be 'profile.php?username=XXX'
Then in your code you will presumably need to perform a simple database query to get the id from the user name. Should be about 5 lines towards the top of the code in profile.php and the rest can remain unchanged Beware that this demands unique user names in case you werent before, and some special characters might be a problem with get parameters in the URL? For most user names, uniqueness is required and special characters are not allowed, so hopefully this isnt a problem.
Keep id but make it a POST peremeters. URL would be 'profile.php'
POST parameters are similar to get but they are not visible to the user because they aren part of the URL string. Just switch $_GET to $_POST in profile.php. The code will still identify users by id, but they won't be able to see their IDs.
In either situation you will need to change any incoming links.actions that lead to the descrubed URL that we are changing.
EDIT - Christian's has a good suggestion which is similar to #1 above but would allow for nicer looking URLs like profile/XXX rather than 'profile.php?username=XXX'. Code within profile.php would ne hte same, his changes are in profile.php are the same

Categories