Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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 20-Jan-10 21:45pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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();
}
}
  Permalink  
Comments
Oubaha at 22-Mar-13 19:10pm
   
how to convert to asp.net
 

 
$texte=$_POST['Req'];
$TGras=array();
preg_match_all("#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";
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>";
?>
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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();
}
}
}
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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
 
 items = items + "{\"s\": \"0\",\"f\": \"" + message.userfrom.ToString() + "\",\"m\": \"" + message.message + "\"}, ";
 
Thanks.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 11

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
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 9

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[^]
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 424
1 OriginalGriff 375
2 CHill60 230
3 CPallini 220
4 Maciej Los 210
0 Sascha Lefévre 75
1 OriginalGriff 70
2 F-ES Sitecore 55
3 Peter Leow 50
4 Dave Kreskowiak 40


Advertise | Privacy | Mobile
Web03 | 2.8.150327.1 | Last Updated 18 Feb 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100