PDA

View Full Version : How to make an out.php script?



Mr. Pink
09-28-2008, 07:53 PM
Hello,

I searched the forums but could not find the exact explanations for what I need. I did find this thread http://www.websitepublisher.net/forums/showthread.php?s=&threadid=336 and some others, but since I am not a programmer I don't understand what needs to be done, unless every step of the process is described.

Basically, I would like to create a database of outgoing links, and an out.php file, just the same way it is done on this site.

My question is, how is that done?

(I am very good at following instructions, but unfortunately I do need every step of the process explained, sorry...)

Thank you in advance for your help...


PS - This is a great site, by the way. I read about 50% of the articles in the past 2 day, since I joined. Really informative. Thank you, Chris. :)

Chris
09-29-2008, 08:22 AM
Do you understand enough to copy & change a php script?



<?

$db = mysql_connect("localhost", "username", "password");
if (!$db) { echo( "<P>Unable to connect to the " . "database server at this time.</P>" ); exit();}

mysql_select_db("database_name", $db);


$result1 = mysql_query("SELECT * FROM redirect_links where LinkID = '$LinkID'", $db);
if (!$result1) { echo("<P>Error performing query: " . mysql_error() . "</P>");
exit();
}
$numResults = mysql_num_rows($result1);
if($numResults != 0){

while($list = mysql_fetch_array($result1)){
$url = $list[url];
}
}
mysql_free_result($result1);

header("Location: $url");
?>

Mr. Pink
09-29-2008, 11:05 AM
Thanks for looking into this, Chris.

Here is what I did so far.

In my Control Panel, I set up a new MySQL database, gave it a name, set up a user and added that user to the new database. I checked all permissions.

In the script, I changed the "username" "password" and "database_name"
I left "localhost" as is (or am I supposed to change that, too?).

On my server, I created a new folder called "scripts" and then I put the out.php script in it.

Is all the above correct?



Now I don't know what to do next.

Basically, where and how do add URL's into the database and how do I manage the database in general?

Sorry if this is all simple stuff, but I have no experience with MySQL or PHP. I've always only worked with CGI scripts and followed whatever installation instructions came with them.


Thank you in advance for your help.

Chris
09-29-2008, 04:36 PM
Yes, all of the above is correct.

Next step, you need to get into phpMyAdmin - look for a link in your control panel. It is a front end for your database.

once you get there browse for your database, then create a new table called

redirect_links

add 2 fields to that table

LinkID
url

for LinkID make it an mediumint field with a length of 8, then under extra select "auto increment" and at the far right for the radio boxes check primary key

for url make it a varchar field with a length of say 200.

then you can insert into this database from phpmyadmin urls, and then browse the database to see the LinkIDs, and reference them with your redirect script.

Mr. Pink
09-29-2008, 06:31 PM
Chris,

First of all, thank you for taking the time to help me out.

I set-up everything as instructed and was able to insert a couple of URLs into the database. However, I'm afraid something is still not right. I spend about an hour troubleshooting, but no luck.

To run the script I just point my browser to www.my-site.com/scripts/out.php?LinkID=1 (I believe this is correct). The browser idles for several seconds and then just stops idling. Nothing happens, there is just a blank browser page with no source code in it. I even tried setting permissions to the script to 755 (it defaulted to 644) and still nothing.

I tried several other things, changing things in the script, but then the browser just returned error pages.

Any idea what I might have missed? :confused:

Mr. Pink
09-30-2008, 02:03 AM
Well... I've been struggling for a few more hours with trying to set up this script. But at least I have some news that may help determine what is wrong, and hopefully how to fix it.

I entered 3 URLs into the database. If I change the LinkID of the 1st URL to the value 0, then the script forwards to that URL. But it forwards to that URL regardless which LinkID I point my browser to. So, if I point it to a non existent LinkID (like 6) it will go to the 1st URL on the list, and if I point it to 2 or 3, it also goes to the 1st URL, while ignoring the actual LinkIDs 2 or 3. Then, when I change the LinkID of the 1st URL back to 1, the script doesn't work again.

Does this information help resolve what is not set up correctly?

Chris
09-30-2008, 05:39 AM
try this.

at the top of out.php add:

echo "ID: ".$LinkID;
echo "<br>Global ID: ".$_GET['LinkID'];

I'm assuming you're accessing out.php like this

/scripts/out.php?LinkID=2

and remember... it is case sensitive.

if you want it to be simpler

put $LinkID = $id;

at the top then you can access it like this

/scripts/out.php?id=2

But tell me what those echos output.

Mr. Pink
09-30-2008, 08:02 AM
Chris,

I did that test.


I'm assuming you're accessing out.php like this

/scripts/out.php?LinkID=2

and remember... it is case sensitive.

Correct, I am accessing like this (case sensitive) so I did not add that additional line on top. I only added the echos.


...But tell me what those echos output.

With the added 2 echos, when calling up LinkID=2, the browser returns this:


ID:
Global ID: 2
Warning: Cannot modify header information - headers already sent by (output started at /home/shark/public_html/scripts/out.php:3) in /home/shark/public_html/scripts/out.php on line 25


Since the error message mentions line 25, I thought I'd include it here. Line 25 says this:

header("Location: $url");

Thanks...

rpanella
09-30-2008, 01:52 PM
You should change this line:


$result1 = mysql_query("SELECT * FROM redirect_links where LinkID = '$LinkID'", $db);

to this:


$result1 = mysql_query("SELECT * FROM redirect_links where LinkID = '" . addslashes($_GET['LinkID']) . "'", $db);

and it should work fine. Of course remove those 2 echo's that you used for testing.
________
VAPORIZERS (http://vaporizers.tv/)

Chris
09-30-2008, 02:47 PM
Yup, do what Russel posted and it'll work.

The issue is your server has a php setting called "register_globals" turned off. They do this for security reasons on shared servers.

So any variable passed will need to be accessed via an array such as $_GET or $_POST

Mr. Pink
09-30-2008, 08:14 PM
:thumbs_up :) It worked!

Thank you very much for helping me out. I'm sure I'll have some more questions, but for now the most important thing is that it works.

Mr. Pink
10-03-2008, 10:20 AM
I did warn I'd have more questions, and here it is. :)

I want to make a separate MySQL database only for my Amazon links. to make it easier for me to know which URL leads to which book, I would like to make the DB with 3 columns (LinkID, title, url). This way when I log onto my CP, and look at the list, I will have the boot titles right next to each LinkID.

So, I know how to make this now, but I want to check with some technical details, first.

1 - I will make a copy of the out.php script, and name it amazon.php. Do I have to add any lines in that new script, since the DB will have an extra column?

2 - What settings do I have to use when I create my new "title" column/field? Do I make it a varchar field? any other settings for this field?

Thank you...

Chris
10-03-2008, 01:15 PM
1. No - you're not using the title for anything so no.

2. Yes, make it a varchar field, with a length long enough to handle your longest title, no other settings are necessary.

Chris
10-03-2008, 01:16 PM
by the way, you may want to go through this tutorial

http://www.websitepublisher.net/article/php-mysql-intro/

Mr. Pink
10-04-2008, 11:26 PM
Thanks for your reply, and also for the link to that article. I read it and found it to be helpful and also found some other interesting articles linked form it.

I now have some additional questions, but they will be better categorized in new threads.

Mr. Pink
10-05-2008, 01:01 AM
I just came up with an idea, as an addition to the redirect PHP script.

I think it is in the best interest of the webmaster to hide the fact that the outgoing links are just referencing a database. So, I found a script online that hides the status bar message, so when a visitor hovers over the link the status bar does not display the URL.

Here is the code:


<script>

//Hide status bar msg II script- by javascriptkit.com
//Visit JavaScript Kit (http://javascriptkit.com) for script
//Credit must stay intact for use

function hidestatus(){
window.status=''
return true
}

if (document.layers)
document.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT)

document.onmouseover=hidestatus
document.onmouseout=hidestatus
</script>

You just have to place into the HEAD section of your page, or place the script in an external file and place a tag in the HEAD that points to that script.

(I left the credit intact, as the script code requests, so I guess it is OK to post someone else's script, right?)

Mr. Pink
08-17-2009, 10:30 AM
Sorry to be reviving this old thread, but i like to keep everything consolidated for future reference.

I just set up a new out.php script for my new site, just like explained above and like i did before. But now I can't make it work.

If you go to my "links" page http://sharpsandflats.com/links_01.html and click on the first link "Alspach's Mathematics & Poker Page..." you will see error message "Unable to connect to the database server at this time."

The only other link that is in the database, at this time, is the 4th link on the list "Faro King..." and I get the same error.

I see that this error message is spelled out at the top of the out.php script.


<?php

$db = mysql_connect("localhost", "username", "my_password");
if (!$db) { echo( "<P>Unable to connect to the " . "database server at this time.</P>" ); exit();}

mysql_select_db("database_name", $db);


$result1 = mysql_query("SELECT * FROM redirect_links where LinkID = '" . addslashes($_GET['LinkID']) . "'", $db);
if (!$result1) { echo("<P>Error performing query: " . mysql_error() . "</P>");
exit();
}
$numResults = mysql_num_rows($result1);
if($numResults != 0){

while($list = mysql_fetch_array($result1)){
$url = $list[url];
}
}
mysql_free_result($result1);

header("Location: $url");
?>


Of course, I tried troubleshooting and the only thing that I could find that looked odd was this...

I go to my CP>PHPmyAdmin and I go to my database. I clicked on the tab "operations" and at the bottom there is a red box with title "ERROR" and text "The additional features for working with linked tables have been
deactivated. To find out why click here."

When I click on "click here" a page opens and says:

PMA Database ... not OK[ Documentation ]
General relation features Disabled

The word "Documentation" is a link, but when I click on it I go to a huge page that just looks like a genera PHP documentation and is entitled "phpMyAdmin 2.6.4-pl2 Documentation".

This documentation page is huge, but clicking on that link opens the page at the following paragraph:


See the Linked-tables infrastructure section in this document to see the benefits of this infrastructure, and for a quick way of creating this database and the needed tables.

If you are the only user of this phpMyAdmin installation, you can use your current database to store those special tables; in this case, just put your current database name in $cfg['Servers'][$i]['pmadb']. For a multi-user installation, set this parameter to the name of your central database containing the linked-tables infrastructure.

This is where I'm stuck.

Any ideas what is wrong?

Thanks...

Mr. Pink
08-17-2009, 10:43 PM
I Just noticed something else that may help resolve this issue.

When I set up the MySQL database for my other sites I had to also set up a user with password, and then add that user to the database.

But this hosting company has a different CP. I only had to set up the database with a password and there is no way to add the user anywhere.

Does that mean that the first line of the pot.php script needs to be changed?

Chris
08-18-2009, 05:27 AM
Try and see, sometimes a CP will just use your account username/pw for your mysql connection.

Mr. Pink
08-18-2009, 07:45 AM
I tried all that in all combinations I could think of.

I changed the first line...


$db = mysql_connect("localhost", "username", "my_password");

...so that username and password were those I use for FTP. But since the CP did require me to create a password I also used the FTP username with the new password.

Nothing else I can think of. Any ideas?

Mr. Pink
08-18-2009, 02:49 PM
Good news.

My problem has been resolves...