Calling a function via URL - possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Calling a function via URL - possible?

Post by hairyjim »

Hi guys,

Is it possible to call a function via a URL?

I have the following code and what I want to be able to do is call the NewsList function via a URL.

The reason being I want to merge two php files I have created, one shows a listing of all news articles and another which shows the article detail. So the idea is when a user initially goes to the file it loads the NewsList function, and when they click on a record it will lod a ShowDetail function. (Not created yet!)

At the minute I am struggling to even call the ShowList function from the URL bar. Am I completly on the wrong track here?

Code: Select all

<?php

OpenConnection();

echo "this is a test line";

function OpenConnection() 
&#123;
	Global $dbconn;
	Global $result;
	$dbconn = mysql_connect("localhost", "user", "password");
	$result = mysql_select_db("database", $dbconn);
	if ( $result == false)
		&#123;
			echo mysql_error();
		&#125;
&#125;

function NewsList() 
&#123;
$sql = "Select sid, title, cdate from table_news order by cdate desc";
		$result = mysql_query( $sql  );
		if ( $result != false )
		&#123;
				while ( $data = mysql_fetch_assoc( $result ) )
				&#123;
					echo "<table width="80%" border="0" cellspacing="0" cellpadding="0">";
  					echo "<tr>" ;
    				echo "<td width="70%"><b><a href="news_detail.php?id=".$data&#1111;'sid']."">".$data&#1111;'title']."</a></b></td>";
    				echo "<td width="30%">$data&#1111;cdate]</td>";
  					echo "</tr>";
					echo "</table>";
				&#125;
		&#125; else &#123;
				echo mysql_error();
		&#125;
&#125;
	
?>
LonelyProgrammer
Forum Contributor
Posts: 108
Joined: Sun Oct 12, 2003 7:10 am

Post by LonelyProgrammer »

Quick sample

a.php

echo "<a href=\"b.php?action=showlist">";
b.php
[/quote]
$action = $_GET["action"];
if ($action=="showlist") Newslist();

This is the gist of it.
This is actually quite dangerous, for it allows people to edit the URL (if they have the address bar on) and call other functions directly.
hairyjim
Forum Contributor
Posts: 219
Joined: Wed Nov 13, 2002 9:04 am
Location: Warwickshire, UK

Post by hairyjim »

Cheers Mate. I'll give it a go.

Soooo simple when someone tells you how ;)
Post Reply