I am using a sms site's api to provide free sms to my site's visitors, as this site has provided me http api and based on that i developed these 2 following pages.

freesms.html

<html>
<head><title>Send Free Sms</title>
<script language="Javascript">
function validate()
{
var valid = true;

if (document.formvalidate.FieldData1.value == "")

{
alert("Please enter the mobile no.");
document.formvalidate.FieldData1.focus();
return false;
}
else if (document.formvalidate.FieldData2.value == "")

{
alert("Please enter the text message");
document.formvalidate.FieldData2.focus();
return false;
}
return valid;
}
</script>
</head>
<body>
<form name="formvalidate" method="post" action="free_sms.php">
<table width="448" border="0">
<tr>
<td width="278">Recipients Mobile no.(Dont add 0 or +91)</td>
<td width="160"><input type="text" name="FieldData1" value="" maxlength="10" size="10"></td>
</tr>
<tr>
<td>Your Message</td>
<td><textarea name="FieldData2" cols="20" rows="5"></textarea></td>
</tr>
<tr><td>&nbsp;</td><td><input type="submit" class="btn" value="Send Sms Now!!" name="Submit" onclick="return validate();"></td></tr>
</table>
</form>
</body>
</html>

free_sms.php

<?php
session_start();
set_time_limit(0);

$server_path = "/home/mysite/public_html/"; 
//$server_path = "../";

require_once($server_path."inc/db_details.php"); 
require_once($server_path."autoload.php"); 
require_once($server_path."inc/mysql_wrapper.php"); 

$DB=new DBClass($dbhost,$dbname,$dblogin,$dbpass); 

function logUser()
{
    $ip = $_SERVER['REMOTE_ADDR'];
    $time = time();

    $query = "INSERT INTO `sms_send_log` (`ip`, `time`) VALUES('$ip', $time)";
    mysql_query($query);
}

function canUserSendSMS()
{
    $ip      = $_SERVER['REMOTE_ADDR'];
    $time    = time();
    $seconds = 86400 * 7; // how many days before they can send again
    $maxsent = 5;

    $query = "SELECT COUNT(*) as num_sent FROM `sms_send_log` WHERE `ip` = '$ip' AND $time - `time` < $seconds";
    $result = mysql_query($query);
    $row    = mysql_fetch_array($result);

    if ($row['num_sent'] >= $maxsent) {
        return false;
    } else {
        return true;
    }
}


$mobile = $_POST['FieldData1'] ;
$text = $_POST['FieldData2'] ;
$username = "myusername";//put your own username instead of yourusername
$password = "mypassword";//put your own password instead of your yourpassword
$message = urlencode($text);
$handle = fopen("http://www.smsgatewaycenter.com/library/send_sms_2.php?UserName=$username&Password=$password&Type=Individual&To=$mobile&Mask=Sender&Message=$message","r");
echo "Message has been Sent";
logUser();

?>

sms_send_log table structure is like this, ip is varchar and time is integer.

Now in this script, I tried to allow maximum 5 sms per visitor for a period of 7 days. this script is not working as am not parsing the return statement properly, require help on this and also, the sms provider has given me 160 characters sms space, in that i want to append my own signature as "Free Sms Courtesy: Mysitename"

How do i achieve this, please help and any help will be appreciated.

Try to add:

$result = canUserSendSMS();
if($result == true)
{
    # send SMS code
}

else
{
    # exit
}

A suggestion: don't relate only on IP, most of the times users IP will change at next reconnect to internet, or it can be also changed by who wants to send more SMS and trick your limitation. And also consider that two, or more, users can share the same IP if they connect from the same LAN.

Thanks so much CEREAL. It worked like a charm. And also I need help in appending a signature in the message ($text = $_POST ;) how do i do that? please help

If I understood your question, write this:

$text = $_POST['FieldData2'];
$text .= ' your signature';

bye :)

You can use curl function also for sending web based SMS

Hey Cereal, Thanks so much, last night, I figured out this and really appreciate all your help.

You are welcome!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.