PHP Create temporary link to page/file [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
I am working on a website project, where you can login.
I want to make a system where you can request a file, but the link to the file should only be valid for a certain amount of time. After the certain amount of time the link should expire and stop working (maybe a show a 404 error).
-I am using php and MySQL database
Thank you!
BTW: I am new on this platform

Start with these 2 tables.
Whenever you upload file or manually add it, add an entry in the files table.
Whenever someone request a file, generate a link for it in links table like https://host/download.php?id=the_random_string which will point to the real file_id, with an expiry date.
files
id
name
physical_path
links
id
file_id
random_string
expiry
So now you should have a file download.php (pseudo code)
$str = $_REQUEST['id'];
$sql = "select file_id from links where random_string=$str AND expiry hasn't passed";
$file_id = $row->file_id;
$sql = "select physical_path from files where id = $file_id";
$physical_path = $row->physical_path;
do_download($physical_path);

You can accomplish that using two ways...
First Way
You can add timestamps on the URL where this URL is valid for a specific time (You must take care of changing this timestamp by the user) so, I recommend adding the timestamp as an arbitrary/inapprehensible string where the user can not change.
Second Way
You can add the timestamp in the database with each file and before accessing the file you must check the stored timestamp if was exceed the current timestamp or not.

Related

How to dynamic generate pages in php? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I'm doing a web app with PHP in which users have different number of tabs in the menu depending on a certain number of things, one user can have one tab in the menu where another user has ten.
I've managed to dynamically generate the menu items but now I don't know how to create files (pages) where the user gets redirected once he presses on any of the menu tabs.
All these files look the same the only thing that changes are the text, just some strings.
Summing up, what I want to do is:
Generate and destroy files automatically when the user has to consult something
How to link the menu tabs (href) to them automatically
The best would be to use a PHP page as template you could call it.
Lets say for example a user is logged in and clicks the view data page (data.php)
on your link you could have something like this:
and in data.php:
<?php
$USER_ID = htmlspecialchars($_GET["id"])
//SQL Request to get user priviledge and authentificate
//OR
//Check the cookie session for information
if (user_priviledge == 0)
{
//
//Show only data for 0 users
//
}elseif (user_priviledge == 1) {
//
//Show only data for 1 users
//
}
?>
This may not be the solution you wanted but your question wasn't very clear either so if you need any other information make sure to bring more precision.
The condition which are using for tab use same condition to show page. But add that condition in your text.Because your text is changing not page I think.
I this this might help you.
since all files will look the same and only variables will change, you can create one php file and just send the necessary data when a user clicks on the tab to assign to the variables in that page. You do not need to keep on creating custom pages for each and every tab.
Provide some code which you have written for this so that we can help you further

PHP - how to generate a URL on save like JSFiddle does? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to recreate something you might know from imgur or jsfiddle - you just type something or upload images, then you click "save" or "upload" and then you get a original link in form of imgur.com/smth or jsfiddle.net/smth. How is this done? I've created simple website where I can write notes and save them and it works perfectly well for myself, but I wanted to share it with some of my friends and I have absolutely no idea how can the "save" button attach their changes to new URL every time they press it? I know this is pretty basic but I couldn't find anything on the matter online and I really have no idea how to handle this issue, excepting I'll be writing their changes to different SQL rows.
Thanks for any clues :)
If you're trying to create something and let people edit it and keep the same link you could do it with PHP and mySQL.
I think the easiest way would be to have one page to create the initial note and one to update/view it. Once you create it the save button would add it to a database with a unique id of some kind like an auto increment number. To send the page you would use a link like www.somepage.com/info.php?id=1. The "id=1" part would be the unique id within the database. In info.php, you would need to use $get_["id"] and set that to equal the database id row and load the info from there. Then you could allow edits to that row from that page. You would have to set the save button to update the row.
To get something like somepage.com/smith, if you're on a linux server, you would use htaccess and mod_rewrite.
This is a really down and dirty gist of how you could do this, but it should send you in the right direction.

php to my SQL insert [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i have a small problem which i cannot find a way to overcome..
i am making a website for car's dealer, i made an insert page which inserts the cars details, i want the car's picture to be saved in a different directory for each car with the name of the folder's name being the car's ID.. i had no problem setting it up this way.
the problem is:
If i take the last ID in the database on the insert page first load then add one to it and put it in the insert statement there is no guarantee that when i upload the data the ID will be free anymore.. even if the last ID is taken at the update time as after the query comes back there could have been an insertion before the DB receives the data for insertion... the problem here is it will not be possible to insert the record anymore... the question here is: is it possible to reserve and ID somehow?
If i made it insert the record first then go to another page for image insertion then how will i be able to track it down, i wont have a way to correspond the uploaded images to the ID since there might be an insertion after my insertion and the last ID will no longer be for the car i have uploaded, so if i get the last ID and upload the images then the images will be uploaded to the wrong directory... the question here is: how can i track it?
or is there another way of doing this?
Use MySQL AutoIncrement feature in the DB table, for ID column
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
And then use mysqli last insert ID, to get the last ID from DB
http://php.net/manual/en/mysqli.insert-id.php
When you have that last (latest) ID, you can reuse it in your PHP in all places you need

Create unique link (one time click) with expiration time [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I want to create unique links (one time click) for my newsletters.
Look the steps:
I will create a link, or can be a script that will create the link http://www.example.com/page.php?unique=email#email.com
My subscriber will receive a newsletter which contains this link.
They click/visit this newsletter/link.
If they want visit it again they can't, the link will be expired, and the subscriber will be redirected to other page http://www.example.com/expired.php
So is there any chance to create this? How can this be accomplished?
I have this code
<?php
include("variables.php");
$password = trim($_SERVER['QUERY_STRING']);
if($password == ADMIN_PASSWORD) {
// Create a new key
$new = uniqid('key',TRUE);
if(!is_dir('keys')) {
mkdir('keys');
$file = fopen('keys/.htaccess','w');
fwrite($file,"Order allow,deny\nDeny from all");
fclose($file);
}
$file = fopen('keys/keys','a');
fwrite($file,"{$new}\n");
fclose($file);
?>
This script is located in a file named generate.php and if I access this url ( http://www.example.com/generate.php?1234 - 1234 is the password to generate a unique ID) will generate a unique link like this http://www.example.com/page.php?key525e1200e3a5f9.19949496 that will be only 1 time available!
Now what I want is, to access http://www.example.com/generate.php?1234&s=email#email.com
And this this generate a unique link http://www.example.com/page.php?key525e1200e3a5f9.19949496&s=email#email.com
This is for a protected download page! I need to add the user email to the download page, and this to be generated by link. 's' is the variable that will be get by the download page using $_GET['s']
Understand what I mean?
I would do this using there email.
Then do a PHP page with the following statements.
Request the GET (in this case the email address) and check if it exists in database table (lets call this - hasvisited).
IF this exists then redirect to page you want.
If doesnt exist then...
1.) Add it to DB.
2.) display 1 time page.
When step 1 happens. it will stop the user from visiting again.
If you want to restrict this to a group of specific users - as the above solution people could theoritcally type anything to get past this...
I would create table with the users already and do an IF state on a column called 'visits' and if 1 or less allow and add +1 to visits column. if above 1 visit then redirect.
I hope this make sense - should also be fairly simple to implement using PHP, MySQL DB and simple IF statement.

Save client-side changes to server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a very simple site (PHP) that allows user changes (via jQuery) that modify elements' classes, and add/delete rows from a table.
I want to write these changes to the actual PHP file in question on the server every time a user makes a change, so that when the page is refreshed or revisited, the changes remain intact.
What's the simplest / best way to accomplish this? Thank you!
For addition/deletion of rows, fire ajax call and update records associated with the user. This is in addition to client-side (jQuery/JavaScript) changes to page. Whenever they revisit/refresh the page, data will be fetched from db, and as such will always be latest.
When you say that users modify elements' classes, I assume you mean their page's looks and styling (like some sites have on-page options to change background color, font-sizes). If that is the case, I suggest you:
Create a list of all possible states and store them in a new table in db, say display_options. You could store details like option_type (e.g. background color, font-size etc) and option_back_value (e.g. #FFF, #000 etc).
Create a new table say user_display_options where you store things like user_id, option_type and their chosen option_id.
While loading page, do a join on these tables using user_id. Then while creating your page, conditionally add classes etc to the page.
When user edits page, fire ajax call sending required data like user_id, option_type, option_id. As mentioned for other task, these changes to db will be in addition to client-side changes.
Also read #David 's comment to your question - it clears an important concept.

Categories