Im trying to pass some information via cookie to my subdomain
in the WordPress login file, wp-login.php I create cookies everytime user login
$outlaw = $_POST['pwd'];
$inlaw = $_POST['log'];
$exp = time()+86400*10;
setcookie("lauthUr", $inlaw, $exp, "/", "mysite.com");
setcookie("lauthPd", $outlaw, $exp, "/", "mysite.com");
in my subdomain which is panel.mysite.com
I run this code in javascript to check if cookie exist via console
function getCookie(name) {
var dc = document.cookie;
var prefix = name + "=";
var begin = dc.indexOf("; " + prefix);
if (begin == -1) {
begin = dc.indexOf(prefix);
if (begin != 0) return null;
}
else
{
begin += 2;
var end = document.cookie.indexOf(";", begin);
if (end == -1) {
end = dc.length;
}
}
// because unescape has been deprecated, replaced with decodeURI
//return unescape(dc.substring(begin + prefix.length, end));
return decodeURI(dc.substring(begin + prefix.length, end));
}
function doSomething() {
var myCookie = getCookie("lauthUr");
if (myCookie == null) {
alert('no');
}
else {
alert('yes')
}
};
doSomething();
I get no alert
can you tell me whats the problem ?
Update
this didnt work either
setcookie("lauthUr", $inlaw, $exp, "/", ".mysite.com");
setcookie("lauthPd", $outlaw, $exp, "/", ".mysite.com");
Related
I have been trying to figure out why one of my variables attaches some garbage value the second time I make a call in gdb but cannot figure it out. I have diagnosed the problem but don't know how to fix it.
On the first call of "curl http://localhost:8080/cat.html", here is what the variables return:
temp = 0x7fffffff9c74 "/cat.html HTTP/1.1\r\n"
request_target = 0x607080 "/cat.html"
abs_path = 0x7fffffffbdd0 "/cat.html"
query = 0x7fffffff9dd0 ""
This works fine and the terminal does show the html file. However, the second time I make the call is where the problem arises.
On the second call of the same address, the following variables are returned:
temp = 0x7fffffff9c74 "/cat.html HTTP/1.1\r\n"
request_target = 0x607080 "/cat.html\327\254\367\377\177"
abs_path = 0x7fffffffbdd0 "/cat.html\327\254\367\377\177"
query = 0x7fffffff9dd0 ""
And then I will receive the 404 file not found error.
Does anybody know why and where I am accumulating all these random characters ("\327\254\367\377\177")? I do believe that it is coming from the temp variable, but I have tried for hours to fix it but I still haven't been able to.
Below are my "parse" and "load" functions:
bool parse(const char* line, char* abs_path, char* query)
{
// GET /cat.html?name="Alice" HTTP/1.1
if (strncmp(line, "GET ", 4) != 0)
{
error(405);
return false;
}
else if (line[4] != '/')
{
error(501);
return false;
}
else if (strncmp(strrchr(line, ' '), " HTTP/1.1", 9) != 0)
{
error(505);
return false;
}
char* temp = strchr(line, '/');
int after_length = strlen(strrchr(line, ' '));
char* request_target = malloc(strlen(temp) - after_length);
strncpy(request_target, temp, strlen(temp) - after_length);
if ((strchr(request_target, '"') != NULL || strchr(request_target, ' ') != NULL))
{
error(400);
return false;
}
if (strchr(request_target, '?') == NULL)
{
strcpy(abs_path, request_target);
strcpy(query, "");
}
else
{
int q_length = strlen(strchr(request_target, '?'));
if (q_length != 2)
{
strncpy(abs_path, request_target, strlen(request_target) - q_length);
strcpy(query, &strchr(request_target, '?')[1]);
}
else
{
strncpy(abs_path, request_target, strlen(request_target) - 2);
strcpy(query, "");
}
}
free(request_target);
return true;
}
bool load(FILE* file, BYTE** content, size_t* length)
{
if (file == NULL)
{
error(500);
return false;
}
*content = NULL;
*length = 0;
BYTE buffer[BYTES];
ssize_t bytes = fread(buffer, 1, sizeof(buffer), file);
while (bytes != 0)
{
*content = realloc(*content, *length + bytes);
memcpy(*content + *length, buffer, bytes);
*length += bytes;
bytes = fread(buffer, 1, sizeof(buffer), file);
}
return true;
}
I have never done combining ajax and session before. So far i have done something like this.
var sessionvar;
$.ajaxSetup({cache: false})
$.get('test.php' ,function (data) {
sessionvar = data;
alert(sessionvar);
var checkedCbs = $('sessionvar:checked');
if (checkedCbs.length === 4) {
alert("You can only select 3 books");
this.checked = false;
}
});
I want to try set the limitations based on session. Inside the test.php i have something like this
<?php
session_start();
if(isset($_POST['sBorrow']) && $_POST['action']){
if(isset($_SESSION['sBorrow']) && is_array($_SESSION['sBorrow'])){
$sborrow = $_POST['sBorrow'];
$set = $_SESSION['sBorrow'];
}
else{
$set = array();
}
if($_POST['action'] == "SET"){
array_push($set, $_POST['sBorrow']);
$_SESSION['sBorrow'] = $set;
}
else if($_POST['action'] == "UNSET"){
$unset = $_SESSION['sBorrow'];
if(($key = array_search($_POST['sBorrow'], $unset)) !== false) {
unset($unset[$key]);
$_SESSION['sBorrow'] = $unset;
}
}
}
//session_destroy();
if(isset($_SESSION['sBorrow'])){
$countses = count($_SESSION['sBorrow']);
echo $countses;
}
// nothing requested, so return all values
print json_encode($_SESSION);
?>
Inside these i have create some array to store something. Never mind that, i just want to know how to run an ajax with session. Im not sure if my codes is right for implementing that.
I want to check the username availability while users register. I am working on the front end. The backend code was given to me.
These are the php code in signup.php
if (isset($_GET['chkusername']))
JSON_username_avail($_GET['chkusername']);
function JSON_username_avail($username) {
$ret = array();
print json_encode(validate_username($username, $ret));
die();
}
function validate_username($username, & $retval_arr) {
if ($username == NULL)
$retval_arr['E_UserName'] = "NULL_USERNAME";
else if (!username_validation($username))
$retval_arr['E_UserName'] = "INVALID_USERNAME";
else if (!data_not_exists("user", "username", $username, TRUE))
$retval_arr['E_UserName'] = "USERNAME_EXISTS";
return $retval_arr;
}
function username_validation($user) {
$username = str_split($user);
foreach($username as $i) {
$i = ord($i);
if ($i >= 48 and $i <= 57)
continue;
if ($i >= 65 and $i <= 90)
continue;
if ($i >= 97 and $i <= 122)
continue;
return FALSE;
}
return TRUE;
}
function data_not_exists($table, $field, $data, $CSense = FALSE) {
$conn = connect_db();
$data = filter_var($data, FILTER_SANITIZE_STRING);
if ($CSense == TRUE)
$sql = "SELECT * FROM ".$table.
" WHERE ".$field.
"='".$data.
"'";
else
$sql = "SELECT * FROM ".$table.
" WHERE upper(".$field.
")='".$data.
"'";
$result = mysqli_query($conn, $sql);
switch ($result - > num_rows) {
case 0:
return TRUE;
break;
case 1:
return FALSE;
break;
default:
die("500 Internal Server Error: 122");
} //switch
}
Now I dont know that much of php. I created a javascript function to send the username to the signup.php page for validation.
Here is my function
function submit_form() {
var u = document.getElementById("username").value;
$.post("signup.php", {
"chkusername": u
},
function (data) {
var x = data; //here i dont know how to get the return string. Whether it is NULL_USERNAME OR INVALID_USERNAME OR USERNAME_EXISTS.
}, "json");
}
here i am getting the value of x as [object Object].
But i need to store the return message in variable x. I want to know whether it is NULL_USERNAME OR INVALID_USERNAME OR USERNAME_EXISTS. Kindly help me with that.
The username is POSTed but in the PHP you try to access it with $_GET, change to:
if (isset($_POST['chkusername']))
JSON_username_avail($_POST['chkusername']);
Also your validation logic doesn't look right, what if the username is valid and available? I would add an else clause and set a success variable:
function JSON_username_avail($username) {
$ret = array();
print json_encode(validate_username($username));
die();
}
function validate_username($username) {
$retval_arr = array('success' => false, 'message' => '');
if ($username == NULL)
$retval_arr['message'] = "NULL_USERNAME";
else if (!username_validation($username))
$retval_arr['message'] = "INVALID_USERNAME";
else if (!data_not_exists("user", "username", $username, TRUE))
$retval_arr['msessage'] = "USERNAME_EXISTS";
else
$retval_arr['success'] = true;
return $retval_arr;
}
and the ajax:
if(!data.success){
console.log(data.message);
} else {
// valid and available
}
Try,
function(data){
var x = data.E_UserName;
}, "json");
also change your request to GET as per MrCode's answer
function submit_form() {
var u = document.getElementById("username").value;
$.get("signup.php", {
"chkusername": u
},
function (data) {
var x = data.E_UserName
}, "json");
}
I want to add a class to a menuepoint if one path is selected.
It would be easy if every site was its own .php/.html file, however everything is ruled in one .php file and everything is navigated over actions (?action=main, ?action=userinformation).
I am searching for an alternative to get this work with the path and the action.
if (location.pathname == "/index.php") {
$("#main1").addClass("mainActive");
} else if (location.pathname == "/index.php?action=other") {
$("#main2").addClass("mainActive");
}
Maybe you can use like;
var s = location.search;
if (s === "" || s === "?" || s.indexOf("?action=main") > -1)
// main active
else if (s.indexOf("?action=userinformation") > -1)
// userinformation active
// ... and so on
location.pathname doesn't include the query string. But you can combine it with location.search:
var path = location.pathname + location.search;
// "/index.php" + "?action=other"
if(path === "/index.php?action=other") {
// ...
}
I got it working on others but on (Firefox the most important one), it doesn't work. What's wrong in my code ? or what's wrong with Firefox :)
if($_COOKIE['ea1']){
die ("cookies set");
} else {
setcookie('ea1',1,time()+24*60*60);
}
try this:
if($_COOKIE['ea1']){
die ("cookies set");
} else {
setcookie('ea1',1,time()+24*60*60,'/','example.com');
}
you might also think of clearing your browsers cookies before
EDIT: if you are on localhost you might have to use
setcookie('ea1',1,time()+24*60*60,'/',false);
This will work
//Set_Cookie('mycookie', 'visited 9 times', 30, '/', '', '');
function Set_Cookie(name, value, expires, path, domain, secure) {
if (!hasKey()) {
return;
}
var today = new Date();
today.setTime(today.getTime());
if (expires) {
expires = expires * 1000 * 60 * 60 * 24;
}
var expires_date = new Date(today.getTime() + (expires));
document.cookie = name + "=" + escape(value) +
((expires) ? ";expires=" + expires_date.toGMTString() : "") +
((path) ? ";path=" + path : "") +
((domain) ? ";domain=" + domain : "") +
((secure) ? ";secure" : ""); }
function Get_Cookie(check_name) {
var a_all_cookies = document.cookie.split(';');
var a_temp_cookie = '';
var cookie_name = '';
var cookie_value = '';
var b_cookie_found = false;
for (i = 0; i < a_all_cookies.length; i++) {
a_temp_cookie = a_all_cookies[i].split('=');
cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
if (cookie_name == check_name) {
b_cookie_found = true;
if (a_temp_cookie.length > 1) {
cookie_value = unescape(a_temp_cookie[1].replace(/^\s+|\s+$/g, ''));
}
return cookie_value;
break;
}
a_temp_cookie = null;
cookie_name = '';
}
if (!b_cookie_found) {
return null;
} }
I've had issues sometimes with cookies and redirects. Make sure you're setting your Location header BEFORE you're Set-Cookie header for maximum browser compatibility.
Had same problem, this worked for me:
Set a cookie on localhost, use false
setcookie("TestCookie", $value, time()+3600, "/", false);
To delete same cookie use negative time
setcookie("TestCookie", '', time()-3600, "/", false);