Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Any one please convert this php code into Asp.net vb code or C# code

------


<?php

define ('DBPATH','localhost');
define ('DBUSER','root');
define ('DBPASS','password');
define ('DBNAME','chat');

session_start();

global $dbh;
$dbh = mysql_connect(DBPATH,DBUSER,DBPASS);
mysql_selectdb(DBNAME,$dbh);

if ($_GET['action'] == "chatheartbeat") { chatHeartbeat(); }
if ($_GET['action'] == "sendchat") { sendChat(); }
if ($_GET['action'] == "closechat") { closeChat(); }
if ($_GET['action'] == "startchatsession") { startChatSession(); }

if (!isset($_SESSION['chatHistory'])) {
$_SESSION['chatHistory'] = array();
}

if (!isset($_SESSION['openChatBoxes'])) {
$_SESSION['openChatBoxes'] = array();
}

function chatHeartbeat() {

$sql = "select * from chat where (chat.to = '".mysql_real_escape_string($_SESSION['username'])."' AND recd = 0) order by id ASC";
$query = mysql_query($sql);
$items = '';

$chatBoxes = array();

while ($chat = mysql_fetch_array($query)) {

if (!isset($_SESSION['openChatBoxes'][$chat['from']]) && isset($_SESSION['chatHistory'][$chat['from']])) {
$items = $_SESSION['chatHistory'][$chat['from']];
}

$chat['message'] = sanitize($chat['message']);

$items .= <<<EOD
{
"s": "0",
"f": "{$chat['from']}",
"m": "{$chat['message']}"
},
EOD;

if (!isset($_SESSION['chatHistory'][$chat['from']])) {
$_SESSION['chatHistory'][$chat['from']] = '';
}

$_SESSION['chatHistory'][$chat['from']] .= <<<EOD
{
"s": "0",
"f": "{$chat['from']}",
"m": "{$chat['message']}"
},
EOD;

unset($_SESSION['tsChatBoxes'][$chat['from']]);
$_SESSION['openChatBoxes'][$chat['from']] = $chat['sent'];
}

if (!empty($_SESSION['openChatBoxes'])) {
foreach ($_SESSION['openChatBoxes'] as $chatbox => $time) {
if (!isset($_SESSION['tsChatBoxes'][$chatbox])) {
$now = time()-strtotime($time);
$time = date('g:iA M dS', strtotime($time));

$message = "Sent at $time";
if ($now > 180) {
$items .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;

if (!isset($_SESSION['chatHistory'][$chatbox])) {
$_SESSION['chatHistory'][$chatbox] = '';
}

$_SESSION['chatHistory'][$chatbox] .= <<<EOD
{
"s": "2",
"f": "$chatbox",
"m": "{$message}"
},
EOD;
$_SESSION['tsChatBoxes'][$chatbox] = 1;
}
}
}
}

$sql = "update chat set recd = 1 where chat.to = '".mysql_real_escape_string($_SESSION['username'])."' and recd = 0";
$query = mysql_query($sql);

if ($items != '') {
$items = substr($items, 0, -1);
}
header('Content-type: application/json');
?>
{
"items": [
<?php echo $items;?>
]
}

<?php
exit(0);
}
/*
function chatBoxSession($chatbox) {

$items = '';

if (isset($_SESSION['chatHistory'][$chatbox])) {
$items = $_SESSION['chatHistory'][$chatbox];
}

return $items;
}*/

function startChatSession() {
$items = '';
if (!empty($_SESSION['openChatBoxes'])) {
foreach ($_SESSION['openChatBoxes'] as $chatbox => $void) {
$items .= chatBoxSession($chatbox);
}
}


if ($items != '') {
$items = substr($items, 0, -1);
}

header('Content-type: application/json');
?>
{
"username": "<?php echo $_SESSION['username'];?>",
"items": [
<?php echo $items;?>
]
}

<?php


exit(0);
}

function sendChat() {
$from = $_SESSION['username'];
$to = $_POST['to'];
$message = $_POST['message'];

$_SESSION['openChatBoxes'][$_POST['to']] = date('Y-m-d H:i:s', time());

$messagesan = sanitize($message);

if (!isset($_SESSION['chatHistory'][$_POST['to']])) {
$_SESSION['chatHistory'][$_POST['to']] = '';
}

$_SESSION['chatHistory'][$_POST['to']] .= <<<EOD
{
"s": "1",
"f": "{$to}",
"m": "{$messagesan}"
},
EOD;


unset($_SESSION['tsChatBoxes'][$_POST['to']]);

$sql = "insert into chat (chat.from,chat.to,message,sent) values ('".mysql_real_escape_string($from)."', '".mysql_real_escape_string($to)."','".mysql_real_escape_string($message)."',NOW())";
$query = mysql_query($sql);
echo "1";
exit(0);
}

function closeChat() {

unset($_SESSION['openChatBoxes'][$_POST['chatbox']]);

echo "1";
exit(0);
}

function sanitize($text) {
$text = htmlspecialchars($text, ENT_QUOTES);
$text = str_replace("\n\r","\n",$text);
$text = str_replace("\r\n","\n",$text);
$text = str_replace("\n","<br>",$text);
return $text;
}

Thanks in advance
Posted

Please find this, Error in this only is that not returning chat to friend

C#.................

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

public partial class chat : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

if (Request.QueryString["action"] != null)
{
if (Session["chatHistory"] == null)
{
Session["chatHistory"] = new Hashtable(); ;
}
if (Session["openChatBoxes"] == null)
{
Session["openChatBoxes"] = new Hashtable();
}
if (Session["tsChatBoxes"] == null)
{
Session["tsChatBoxes"] = new Hashtable();
}
switch (Request.QueryString["action"].ToString())
{
case "chatheartbeat":
{
chatHeartbeat();
break;
}
case "sendchat":
{
sendChat();
break;
}
case "closechat":
{
closeChat();
break;
}
case "startchatsession":
{
startChatSession();
break;
}
}
}

Hashtable s = new Hashtable();
}
private void chatHeartbeat()
{
/* Add the Code to get the chat from the databse here and assign the result to datatable dtChat */

List<Chattlb> messages = Chating.chat.GetNotRecieved(Session["username"].ToString());
string items = "";

foreach (Chattlb message in messages)
{
if (Session["openChatBoxes"] != null && Session["chatHistory"] != null)
{
if (!((Hashtable)(Session["openChatBoxes"])).Contains(message.userfrom) && ((Hashtable)(Session["chatHistory"])).Contains(message.userfrom))
{
items = ((Hashtable)(Session["chatHistory"]))[message.userfrom].ToString();
}
}

message.message = sanitize(message.message);

items = items + "\"s\":\"0\",\"f\":\"{" + message.userfrom.ToString() + "}\",\"m\":\"{" + message.message + "}\"";

if (Session["chatHistory"] != null)
{
if (!((Hashtable)(Session["chatHistory"])).Contains(message.userfrom))
{
((Hashtable)(Session["chatHistory"]))[message.userfrom] = "";

}
}
if (Session["chatHistory"] != null)
((Hashtable)(Session["chatHistory"]))[message.userfrom.ToString()] += "\"s\":\"0\",\"f\":\"{" + message.userfrom.ToString() + "}\",\"m\":\"{" + message.message + "}\"";
if (Session["tsChatBoxes"] != null)
((Hashtable)(Session["tsChatBoxes"]))[message.userfrom.ToString()] = null; // Unset means clear the session value
if (Session["openChatBoxes"] != null)
((Hashtable)(Session["openChatBoxes"]))[message.userfrom.ToString()] = message.send;


}
if (Session["openChatBoxes"] != null)
{
Hashtable openChatBoxesArray = (Hashtable)(Session["openChatBoxes"]);
for (int i = 0; i < openChatBoxesArray.Count; i++)
{
int chatbox = i;
if (openChatBoxesArray[i] != null)
{
DateTime time = (DateTime)(openChatBoxesArray[i]);
string timeval = "";
TimeSpan Now = (TimeSpan)(DateTime.Now - time);
timeval = time.ToString("g:iA M dS");
string message = "Sent at " + timeval;

if (Now.Minutes > 180)
{
items += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message + "}\"";
if (((Hashtable)(Session["chatHistory"]))[chatbox] != null)
{
((Hashtable)(Session["chatHistory"]))[chatbox] = "";
}
((Hashtable)(Session["chatHistory"]))[chatbox] += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message + "}\"";
((Hashtable)(Session["tsChatBoxes"]))[chatbox] = "1";
}
}
}
}
Chating.chat.UpdateRecieved(Session["username"].ToString());
if (items != string.Empty)
{
items = items.Substring(0, items.Length - 1);
}
HttpContext.Current.Response.Write("{ ");
Response.Write("\"items\":[" + items + "]");
HttpContext.Current.Response.Write(" }");
Response.End();
}
private string sanitize(string text)
{
//text = htmlspecialchars(text, ENT_QUOTES); // Convert the html special characters
text = text.Replace("\n\r", "\n");
text = text.Replace("\r\n", "\n");
text = text.Replace("\n", "<br>");
return text;
}
private int getIndexOf(Hashtable SessionArray, string Value)
{
//try
//{
// return SessionArray.IndexOf((object)Value);
//}
//catch
//{
return 0;
//}
}

private string chatBoxSession(int chatbox)
{

string items = "";

if (((Hashtable)(Session["chatHistory"]))[chatbox] != null)
{ // Check the Session variable
items = ((Hashtable)(Session["chatHistory"]))[chatbox].ToString();
}

return items;
}

private void sendChat()
{
string from = Session["username"].ToString(); // Assign Session user name value to $from value
string to = Request["to"]; // Assign POST FORM 'To' value to $to value}
string message = Request["message"]; // Assign POST FORM 'message' value to $message value


if (Session["openChatBoxes"] != null)
((Hashtable)(Session["openChatBoxes"]))[to] = DateTime.Now.ToString("Y-m-d H:i:s");

string messagesan = sanitize(message);
if (Session["chatHistory"] != null)
{
if (((Hashtable)(Session["chatHistory"])).Contains(Request["to"]))
{
((Hashtable)(Session["chatHistory"]))[to] = "";
//((Hashtable)(Session["chatHistory"]))[getIndexOf(((Hashtable)(Session["chatHistory"])), Request["to"])] = "";
}
}
if (Session["chatHistory"] != null)
((Hashtable)(Session["chatHistory"]))[to] += "\"s\":\"1\",\"f\":\"{" + to + "}\",\"m\":\"{" + messagesan + "}\"";
if (Session["tsChatBoxes"] != null)
((Hashtable)(Session["tsChatBoxes"]))[to] = null;

/* Add Insert Query here*/

Chattlb chatObj = new Chattlb();
chatObj.userto = to;
chatObj.userfrom = from;
chatObj.message = message;
chatObj.send = DateTime.Now;
chatObj.recd = 0;
Chating.chat.InsertChat(chatObj);

Response.Write("1");
Response.End();
}
private void closeChat()
{
int chatbox = 1; //value of chatbox
try
{
((Hashtable)(Session["openChatBoxes"]))[chatbox] = null;
}
catch
{
}
Response.Write("1");
}
private void startChatSession()
{
string items = "";
if (Session["openChatBoxes"] != null)
{ // Check the Session variable
Hashtable openChatBoxesArray = (Hashtable)(Session["openChatBoxes"]);
for (int i = 0; i < openChatBoxesArray.Count; i++)
{ // Loop the session array
items += chatBoxSession(i); // Append to Items variable
}
}
if (items != string.Empty)
{
items = items.Substring(0, items.Length - 1);
}
HttpContext.Current.Response.Write("{ ");
HttpContext.Current.Response.Write("\"username\": \"" + Session["username"].ToString() + "\","); // Display the Username Which is set in the session
HttpContext.Current.Response.Write("\"items\": [");
HttpContext.Current.Response.Write(items);
HttpContext.Current.Response.Write("]");
HttpContext.Current.Response.Write("}");
Response.End();
}
}
 
Share this answer
 
Comments
Oubaha 22-Mar-13 19:10pm    
how to convert to asp.net



T(.)*?T#",$texte,$TGras);
print_r($TGras[0]);
echo "
";
$texte=preg_replace('/[,\.]/','',$texte);
$texte=trim($texte);
$tableau=preg_split("/( )+/",$texte);
echo $texte."<br>";
print_r($tableau."<br>");
$T=array();
$T2=array();
$total=count($tableau);
foreach($tableau as $e){
if(!key_exists($e,$T))
{
$T[$e]=1;
}else{
$T[$e]++;
}
}
$M=sizeof($tableau)/2;
for($i=0;$i<sizeof($tableau);$i++){
if(!key_exists($tableau[$i],$t2))
{
$t2[$tableau[$i]]=($i+1-$m)*($i+1-$m);
}else{
$t2[$tableau[$i]]=$t2[$tableau[$i]]+($i+1-$m)*($i+1-$m);
}
}
print_r($t);
print_r($t2);
echo "&lt;table="" border="1">";
echo "<tr><th>Mot</th><th>nbr occurences</th><th>distribution</th></tr>";
foreach(array_keys($T) as $e){
echo "<tr><td>$e</td><td>".round($T[$e]*100/$total,2)." %</td><td>".sqrt($T2[$e])."</td></tr>";
}
echo "</table>";
?>
I am answering to get this out of the unanswered question list. The answer is no, unless you're paying, no-one is going to rewrite all this code using ASP.NET. You'd do better to just define the task and write it from scratch, PHP sucks.
 
Share this answer
 
this php code from http://anantgarg.com/2009/05/13/gmail-facebook-style-jquery-chat/[^]

The convert php to c#/vb not too tough but here problem will not solve from this conversion. here added some js & jquery file with php file.

yet...Keep Try!!

The code as after conversion ..(its not my code)
C# code..........................

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;

namespace wwwroot.chat
{
public partial class chathandler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{


if (Request.QueryString["action"] != null)
{
switch (Request.QueryString["action"].ToString())
{
case "chatheartbeat":
{
chatHeartbeat();
break;
}
case "sendchat":
{
sendChat();
break;
}
case "closechat":
{
closeChat();
break;
}
case "startchatsession":
{
startChatSession();
break;
}
}
}
if (Session["chatHistory"] == null)
{
Session["chatHistory"] = new ArrayList(); ;
}
if (Session["openChatBoxes"] == null)
{
Session["openChatBoxes"] = new ArrayList();
}
if(Session["tsChatBoxes"]==null)
{
Session["tsChatBoxes"] = new ArrayList();
}
ArrayList s = new ArrayList();
}



private void chatHeartbeat()
{
/* Add the Code to get the chat from the databse here and assign the result to datatable dtChat */

GenericCollection<Chat> messages = Chat.GetNotRecieved(User.Identity.Name);
string items = "";

foreach (Chat message in messages)
{
if (!((ArrayList)(Session["openChatBoxes"])).Contains(message.UsernameFrom) && ((ArrayList)(Session["chatHistory"])).Contains(message.UsernameFrom))
{
items = ((ArrayList)(Session["chatHistory"]))[((ArrayList)(Session["chatHistory"])).IndexOf((object)(message.UsernameFrom))].ToString();
}
message.Message = sanitize(message.Message);

items = items + "\"s\":\"0\",\"f\":\"{" + message.UsernameFrom.ToString() + "}\",\"m\":\"{" + message.Message + "}\"";

if (!((ArrayList)(Session["chatHistory"])).Contains(message.UsernameFrom))
{
((ArrayList)(Session["chatHistory"]))[((ArrayList)(Session["chatHistory"])).IndexOf((object)(message.UsernameFrom))] = "";
}
((ArrayList)(Session["chatHistory"]))[getIndexOf(((ArrayList)(Session["chatHistory"])), message.UsernameFrom.ToString())] += "\"s\":\"0\",\"f\":\"{" + message.UsernameFrom.ToString() + "}\",\"m\":\"{" + message.Message + "}\"";
((ArrayList)(Session["tsChatBoxes"]))[getIndexOf(((ArrayList)(Session["tsChatBoxes"])), message.UsernameFrom.ToString())] = null; // Unset means clear the session value
((ArrayList)(Session["openChatBoxes"]))[getIndexOf(((ArrayList)(Session["openChatBoxes"])), message.UsernameFrom.ToString())] = message.Sent;
}
if (Session["openChatBoxes"] != null)
{
ArrayList openChatBoxesArray = (ArrayList)(Session["openChatBoxes"]);
for (int i = 0; i < openChatBoxesArray.Count; i++)
{
int chatbox = i;
if (openChatBoxesArray[i] != null)
{
DateTime time = (DateTime)(openChatBoxesArray[i]);
string timeval = "";
TimeSpan Now = (TimeSpan)(DateTime.Now - time);
timeval = time.ToString("g:iA M dS");
string message = "Sent at " + timeval;

if (Now.Minutes > 180)
{
items += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message + "}\"";
if (((ArrayList)(Session["chatHistory"]))[chatbox] != null)
{
((ArrayList)(Session["chatHistory"]))[chatbox] = "";
}
((ArrayList)(Session["chatHistory"]))[chatbox] += "\"s\":\"2\",\"f\":\"{" + chatbox.ToString() + "}\",\"m\":\"{" + message + "}\"";
((ArrayList)(Session["tsChatBoxes"]))[chatbox] = "1";
}
}
}
}
Chat.UpdateRecieved(User.Identity.Name);
if (items != string.Empty)
{
items = items.Substring(0, items.Length - 1);
}
HttpContext.Current.Response.Write("{ ");
Response.Write("\"items\":[" + items + "]");
HttpContext.Current.Response.Write(" }");
Response.End();
}
private string sanitize(string text)
{
//text = htmlspecialchars(text, ENT_QUOTES); // Convert the html special characters
text = text.Replace("\n\r", "\n");
text = text.Replace("\r\n", "\n");
text = text.Replace("\n", "<br>");
return text;
}
private int getIndexOf(ArrayList SessionArray, string Value)
{
try
{
return SessionArray.IndexOf((object) Value);
}
catch
{
return 0;
}
}

private string chatBoxSession(int chatbox)
{

string items = "";

if (((ArrayList)(Session["chatHistory"]))[chatbox] != null)
{ // Check the Session variable
items = ((ArrayList)(Session["chatHistory"]))[chatbox].ToString();
}

return items;
}

private void sendChat()
{
string from = User.Identity.Name; // Assign Seesion user name value to $from value
string to = Request["to"]; // Assign POST FORM 'To' value to $to value}
string message = Request["message"]; // Assign POST FORM 'message' value to $message value

((ArrayList)(Session["openChatBoxes"]))[getIndexOf(((ArrayList)(Session["openChatBoxes"])), Request["to"])] = DateTime.Now.ToString("Y-m-d H:i:s");

string messagesan = sanitize(message);

if (((ArrayList)(Session["chatHistory"])).Contains(Request["to"]))
{
((ArrayList)(Session["chatHistory"]))[getIndexOf(((ArrayList)(Session["chatHistory"])), Request["to"])] = "";
}
((ArrayList)(Session["chatHistory"]))[getIndexOf(((ArrayList)(Session["chatHistory"])), Request["to"])] += "\"s\":\"1\",\"f\":\"{" + to + "}\",\"m\":\"{" + messagesan + "}\"";
((ArrayList)(Session["tsChatBoxes"]))[getIndexOf(((ArrayList)(Session["tsChatBoxes"])), Request["to"])] = null;


/* Add Insert Query here*/
Chat chatObj = new Chat();
chatObj.UsernameTo = to;
chatObj.UsernameFrom = from;
chatObj.Message = message;
chatObj.Sent = DateTime.Now;
chatObj.Recieved = false;
Chat.Insert(chatObj);

Response.Write("1");
Response.End();
}
private void closeChat()
{
int chatbox = 1; //value of chatbox
((ArrayList)(Session["openChatBoxes"]))[chatbox] = null;
Response.Write("1");
}
private void startChatSession()
{
string items = "";
if (Session["openChatBoxes"] != null)
{ // Check the Session variable
ArrayList openChatBoxesArray = (ArrayList)(Session["openChatBoxes"]);
for (int i = 0; i < openChatBoxesArray.Count; i++)
{ // Loop the session array
items += chatBoxSession(i); // Append to Items variable
}
}


if (items != string.Empty)
{
items = items.Substring(0, items.Length - 1);
}
HttpContext.Current.Response.Write("{ ");
HttpContext.Current.Response.Write("\"username\": \"" + HttpContext.Current.User.Identity.Name + "\","); // Display the Username Which is set in the session
HttpContext.Current.Response.Write("\"items\": [");
HttpContext.Current.Response.Write(items);
HttpContext.Current.Response.Write("]");
HttpContext.Current.Response.Write("}");
Response.End();
}
}
}
 
Share this answer
 
v2
C#
private void chatHeartbeat()
{
/* Add the Code to get the chat from the databse here and assign the result to datatable dtChat */

List<Chattlb> messages = Chating.chat.GetNotRecieved(Session["username"].ToString());
string items = "";

foreach (Chattlb message in messages)
{
if (Session["openChatBoxes"] != null && Session["chatHistory"] != null)
{
if (!((Hashtable)(Session["openChatBoxes"])).Contains(message.userfrom) && ((Hashtable)(Session["chatHistory"])).Contains(message.userfrom))
{
items = ((Hashtable)(Session["chatHistory"]))[message.userfrom].ToString();
}
}

message.message = sanitize(message.message);


//////////////// problem in this line
items = items + "\"s\":\"0\",\"f\":\"{" + message.userfrom.ToString() + "}\",\"m\":\"{" + message.message + "}\"";

/////////////////////////////////////////////

In the Solution 5 the problem is here. The Json Message was not correct so the user cannot receive messages

try using this line

C#
items = items + "{\"s\": \"0\",\"f\": \"" + message.userfrom.ToString() + "\",\"m\": \"" + message.message + "\"}, ";


Thanks.
 
Share this answer
 
I try to use this code, but i am getting the error on SendChat Method

string to = Request["to"]; // Assign POST FORM 'To' value to $to value}

((ArrayList)(Session["openChatBoxes"]))[getIndexOf(((ArrayList)(Session["openChatBoxes"])), Request["to"])] = DateTime.Now.ToString("Y-m-d H:i:s");


This line was getting this error

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index


Can anyone guide me to fix this error
 
Share this answer
 
v3
I think that[^] is you[^] (So you have two accounts?). Why do you still want that code after 2 years?

Anyway check this
.NET Code Conversion - Convert your code - PHP to .NET[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900