PDA

View Full Version : PHP Delete Function



Titan85
10-15-2006, 03:52 AM
Hello, I am making a script that ads links to a .txt file when the form is submitted. I have the ad link part working fine, but I need to know how to go about making a delete function to remove the link from the .txt file. I am not 100% sure this can be done with a .txt file, but let me know either way. Thanks

djr33
10-15-2006, 08:34 AM
Clearing the text file is really easy.
$textfile = "";
Assuming you have used fopen() to set $textfile to the file, and such.
[Also, depending on the method used to open the file (w+/a/etc., I can't remember what's what at the moment), you might be just adding "" to it, not setting it to that, but it's possible.]

If you wish to remove a specific line from the text file, then you would do basically the same, but do this instead:
Assume $textfile is the contents of the .txt, again.
str_replace($link,"",$textfile);

Here's how it works: str_replace(FIND, REPLACE, IN_THIS);

You may also want to use something like
str_replace($link."\n","",$textfile);

That would replace the link, then the line break after it.


Hope this helps.

Titan85
10-15-2006, 01:52 PM
Clearing the text file is really easy.
$textfile = "";
Assuming you have used fopen() to set $textfile to the file, and such.
[Also, depending on the method used to open the file (w+/a/etc., I can't remember what's what at the moment), you might be just adding "" to it, not setting it to that, but it's possible.]

If you wish to remove a specific line from the text file, then you would do basically the same, but do this instead:
Assume $textfile is the contents of the .txt, again.
str_replace($link,"",$textfile);

Here's how it works: str_replace(FIND, REPLACE, IN_THIS);

You may also want to use something like
str_replace($link."\n","",$textfile);

That would replace the link, then the line break after it.


Hope this helps.Ok, I get what you are saying about replacing the link with a blank, but how would I make it so that when a link is added, it also ads a delete option to it. I am thinking that I could use a link ID that autoincrements and then use the ID to reference the link. If that will work, the only thing that i don't know how to do is to make the delete function get the corresponding ID.

blm126
10-15-2006, 03:22 PM
We need to see the code if you want anything but general answers. However, I think the ID plan is a bad idea. Store the text of line to delete in your form, and use that to find the line.

Titan85
10-15-2006, 04:21 PM
Ok, here is the code for you
Processing
<?php
$filename = "featured_links.txt"; // file to write to

$date = date("F j, Y"); //date format

$error = "An error occured while adding new link!";

$success = "Link successfully added, please choose the page you would like to go to below. You will be automatically redirected to the ad links page in 10 seconds.
<br />
<a href=\"http://ad_link.php\">Ad Links Page</a>
<br />
<a href=\"jexhost.com\">Site Home</a>";



// Add Text
$text = stripslashes($_POST['text']);
if (!$text) {
$text = "<b>Untitled</b>";
}

// Add Link
$link = stripslashes($_POST['link']);
if (!link) {
$link = "";
}


// Add Content
if ($content = stripslashes($_POST['content'])) {

$add = '
<!-- Text Ad, '.$date.' -->
<a href="http://'.$link.'">'.$title.'</a>
<br />

';


$links = @file_get_contents($filename).$add;


$file = @fopen($filename, "w");
@fwrite($file, $news);
@fclose($file);
$message = $success;
}
else {
$message = $error;
}
echo "
<html>
<head>
<title>Jexlinks.com | Link Added</title>
<meta http-equiv=\"refresh\" content=\"10;url=ad_link.php\">
".$message."
</head>
<body>
</body>
</html>";


?>
Add Form
<form action="ad_link_process.php" method="post">
Link Text:
<br />
<input name="text" type="text" />
<br />
Link URL:
<br />
<input name="link" type="text" />
<br />
<input type="submit" value="Submit" />
</form>

djr33
10-16-2006, 05:17 AM
search/replace works well to remove a link, since you are removing the same thing you're searching for. ID is only important if you don't have the data you're searching for.

Titan85
10-16-2006, 11:11 AM
search/replace works well to remove a link, since you are removing the same thing you're searching for. ID is only important if you don't have the data you're searching for.The thing is that I don't know the data I am searching for. Each link that is added can ad any data, and I need a delete function that will be able to be added to each new link that is added.

djr33
10-16-2006, 04:19 PM
Yeah. You do know what you're searching for.
When you delete a link, you obviously know the link at that point. Use it to delete it.
Store it as $link, then search/replace $link.
You don't need to hard code the urls into the script.

Titan85
10-16-2006, 08:47 PM
Yeah. You do know what you're searching for.
When you delete a link, you obviously know the link at that point. Use it to delete it.
Store it as $link, then search/replace $link.
You don't need to hard code the urls into the script.What do you mean by not hard storing the link? When the link is written, it looks like this:
<!-- Text Ad, Added October 15, 2006 -->
<a href="#" target="_blank">- Testing Link</a>
<br />And that is the code that is entered, so being that there is no $link, i don't know how i could reference the correct link. There will be hundreds of links, so I need a unique ID to be added to each link so that i can click on the "delete" button and execute the delete function for that link.

djr33
10-16-2006, 10:29 PM
How does a user navigate to the link? It's the href of the link, yes?
Using that href, can store it as $link, through php code, not just waiting for it to show up.... I didn't mean it would automatically be there.

Somehow, you have the url available. Use that url to add the delete link....
http://..../delete.php?url=urlhere
And that should delete it.

There is absolutely no need for an ID as it just ads one step and you'd STILL need the url in the end. Instead of messing with IDs, just use the url itself, which you MUST!!! have available to link the person.

Titan85
10-17-2006, 12:02 AM
How does a user navigate to the link? It's the href of the link, yes?
Using that href, can store it as $link, through php code, not just waiting for it to show up.... I didn't mean it would automatically be there.

Somehow, you have the url available. Use that url to add the delete link....
http://..../delete.php?url=urlhere
And that should delete it.

There is absolutely no need for an ID as it just ads one step and you'd STILL need the url in the end. Instead of messing with IDs, just use the url itself, which you MUST!!! have available to link the person.Ok, i get it now and it makes sense. Just a few minor things. How do i store the link as $link (a variable)? Also, is there any way to have the delete option shown in the admin control panel, but not in the page users see? Perhaps through referer? Thanks

djr33
10-17-2006, 03:53 AM
1. How does the URL appear in the clickable link? Get it from there, then store as $link.
<?php $link = "whateverhere"; ?>
It's easy to set... getting it is harder... but you ARE getting it, so just repeat the process.
Post the code if you need a more specific example.

2. Yes. Displaying it only in the admin cp would be php code. You would just use different code for each. Use the normal one alone and change the code for the admin cp. Or, if it's the same script, use an if to check if the user is admin. If so, then display the delete link.

Titan85
10-17-2006, 11:12 AM
1. How does the URL appear in the clickable link? Get it from there, then store as $link.
<?php $link = "whateverhere"; ?>
It's easy to set... getting it is harder... but you ARE getting it, so just repeat the process.
Post the code if you need a more specific example.

2. Yes. Displaying it only in the admin cp would be php code. You would just use different code for each. Use the normal one alone and change the code for the admin cp. Or, if it's the same script, use an if to check if the user is admin. If so, then display the delete link.Ok, so if i were to use <?php $link = "whatever"; ?>, would i need to make a new php file for each link that is added? Other than that I get what you are saying, thanks

djr33
10-17-2006, 11:48 AM
obviously "whatever" is replaced with the link.
I don't know how you are storing the link, but I know you are. You need to find out how to access that then use it.
no new php file for each link.

php is a server side language that outputs html.
you can have as many sets of php tags (<?php ... ?>) as you want in a document. they are NOT EVER output. The server ("server side" -- get it?) interprets the tags and outputs ONLY html.

You could use the echo command (aka print, etc.) to output the value of the link in that case.


This is getting really complicated.

How much of the script have you written? What does it do at this point?
Please post the script.

Titan85
10-17-2006, 08:54 PM
Ok, here is the code i am trying:
ad_link_process.php

<?php
require ("includes.php");

$filename = "featured_links.php"; // file to write to

$date = date("F j, Y"); //date format

$error = "An error occured while adding new link!";



// Add Text
$text = stripslashes($_POST['text']);
if (!$text) {
$text = "<b>Untitled</b>";
}

// Add Link
if ($link = stripslashes($_POST['link'])) {

$add = '
<?php

$linkurl = "'.$link.'";

if ($user = "jexlinks") {
print ("<a href="'.$linkurl.'">'.$text.'</a> <a href="delete.php">Delete</a>");
}else{
print ("<a href="'.$linkurl.'">'.$text.'</a>");
}
?>

';


$links = file_get_contents($filename).$add;


$file = @fopen($filename, "w");
@fwrite($file, $links);
@fclose($file);
$message = $success;
}
else {
$message = $error;
}

require ("link_added.php");


?>
Delete.php

<?
str_replace("$text", "", "ad_link_process.php");
?>

<html>
<head>
<title>Delete String</title>
</head>

<body>
Link Deleted!
</body>

</html>
Its just simple right now, but the lines are added to featured_links.php without an issue, but nothing is displayed when i try to access that page : / . Not really sure about all the code, but its just the concept i had in my mind

djr33
10-17-2006, 11:55 PM
Heh. You're replacing the string in the php page... that's not a valid variable... just wrong.

str_replace(SEARCH, REPLACE, VARIABLEITSIN);

Titan85
10-18-2006, 12:00 AM
Heh. You're replacing the string in the php page... that's not a valid variable... just wrong.

str_replace(SEARCH, REPLACE, VARIABLEITSIN);No, i am replacing the $text variable so that there is no longer text for the link, i am not replacing any strings.

blm126
10-18-2006, 12:29 AM
Here this may be of use to you.


function deleteline($del,$filename){
$file = file($filename);
ob_start();
for($i=0;$i < count($file);$i++){
if(strpos($file[$i],$del) === FALSE){
echo $file[$i];
}
}
$write = ob_get_contents();
ob_end_clean();
$handle = fopen('list.txt','w');
fwrite($handle,$write);
fclose($handle);
}
//Pass in the search term, then the filename

This function deleteline takes two variables. The first is a search term. In this case that would be $text. And the second is the file name. This will delete the entrie line if it finds that search term. Though it should be able to modify a PHP file, I wouldn't reccomend that(huge,huge,huge security risk)

Titan85
10-18-2006, 12:32 AM
Here this may be of use to you.


function deleteline($del,$filename){
$file = file($filename);
ob_start();
for($i=0;$i < count($file);$i++){
if(strpos($file[$i],$del) === FALSE){
echo $file[$i];
}
}
$write = ob_get_contents();
ob_end_clean();
$handle = fopen('list.txt','w');
fwrite($handle,$write);
fclose($handle);
}
//Pass in the search term, then the filename

This function deleteline takes two variables. The first is a search term. In this case that would be $text. And the second is the file name. This will delete the entrie line if it finds that search term. Though it should be able to modify a PHP file, I wouldn't reccomend that(huge,huge,huge security risk)Ok, i don't really get what all that script is saying, could you explain it, but if it is too great a security risk (even if in protected folder), then no need to explain it

blm126
10-18-2006, 09:24 PM
Oh no, my function is safe. It is your possible use of it(from what I can tell) would be unsafe. As for explaining it. Basically it defines a function(deleteline) that will search in a file and delete any line that matches the search term provided. To understand how it works, just look up the various functions I used.