Jump to content

[SOLVED] Cookie problem


kartul

Recommended Posts

ok, i'm trying to make a thing that users can choose how many results they want to see.

idea is:

default it shows 25.

there is select option to select how many results display per page.

when user selects something, it submits the form and sets cookie with selected number.

now, i have a problem. I don't want submit button but now, how do i check if the form is submitted with php?

heres form:

		<form action="index.php" method="post" name="display">
			Display <select name="show" onchange="this.form.submit()">
				<option value="25">25</option>
				<option value="30">30</option>
			</select>
		</form>

 

and here's php code to set cookie:

    function give_cookie($how_many) {
    	setcookie("display", $how_many, time()+60*60*24*30*365*5);
    }

if(isset($_POST['display'])) {
$show = mss($_POST['show']);

if(is_int($show)) {
	give_cookie($show);
}
}

 

right now, when I select example 30, it submits the form, refreshes the page but nothing else happens.

 

one more thing, if I set display cookie to 25 at the top of the page and then later want to use it, it's blank, and when i refresh the page, then it ok.

if(!isset($_COOKIE['display'])) {
setcookie("display", "25", time()+60*60*24*30*365*5);
}

 

Link to comment
Share on other sites

Also, if (isset($_POST['display'])) might not work because the actual form element will not send data to the server. It's its elements inside that will. So, you can just check isset($_POST['show']) and it will work.

 

Edit-Adding: Naming a form element's purpose is mostly just for JavaScript. You shouldn't name it. It might work in IE (haven't tried it) but I'm guessing future browser might not work.

Link to comment
Share on other sites

What does the mms() function do? Does it really return the value submitted?

mss() is security function. just in case

function mss($value) {
return mysql_real_escape_string(trim(strip_tags(stripslashes(htmlentities(htmlspecialchars($value))))));
}

Link to comment
Share on other sites

Also, if (isset($_POST['display'])) might not work because the actual form element will not send data to the server. It's its elements inside that will. So, you can just check isset($_POST['show']) and it will work.

 

Edit-Adding: Naming a form element's purpose is mostly just for JavaScript. You shouldn't name it. It might work in IE (haven't tried it) but I'm guessing future browser might not work.

ok, i changed $_POST['display'] to $_POST['show']. but it's still just refreshes page and cookie value is still default - 25. maybe problem is somewhere else?

 

you mean <form action="index.php" method="post" name="display"> or <select name="show" onchange="this.form.submit()">?

Link to comment
Share on other sites

ok, i tried following:

just to be sure. this doesn't work.

if(isset($_POST['show'])) {
$show = $_POST['show'];

if(is_int($show)) {
	//give_cookie($show);
	setcookie("display", "30", time()*60*60);
}
}

 

now I tried this:

if(isset($_POST['show'])) {
$show = mss($_POST['show']);

if($show > 0) {
	give_cookie($show);
}
}

ok, this works perfectly now. thank you! and it should be sql injection proof too?

anyway, now I have another issue. default is 25. when I select 30. it submits the form and does it's thing, but i need to refresh the page to make it display 30 results per page.. how do change that so it displays 30 right after submitting the form?

Link to comment
Share on other sites

ok, i tried following:

just to be sure. this doesn't work.

if(isset($_POST['show'])) {
$show = $_POST['show'];

if(is_int($show)) {
	//give_cookie($show);
	setcookie("display", "30", time()*60*60);
}
}

 

now I tried this:

if(isset($_POST['show'])) {
$show = mss($_POST['show']);

if($show > 0) {
	give_cookie($show);
}
}

ok, this works perfectly now. thank you! and it should be sql injection proof too?

anyway, now I have another issue. default is 25. when I select 30. it submits the form and does it's thing, but i need to refresh the page to make it display 30 results per page.. how do change that so it displays 30 right after submitting the form?

 

 

I'm guessing that you do the cookie change after the parsing of the page. Try putting the if(isset($_POST['show'])) block before you echo out all of the items/articles/results.

Link to comment
Share on other sites

ok, i tried following:

just to be sure. this doesn't work.

if(isset($_POST['show'])) {
$show = $_POST['show'];

if(is_int($show)) {
	//give_cookie($show);
	setcookie("display", "30", time()*60*60);
}
}

 

now I tried this:

if(isset($_POST['show'])) {
$show = mss($_POST['show']);

if($show > 0) {
	give_cookie($show);
}
}

ok, this works perfectly now. thank you! and it should be sql injection proof too?

anyway, now I have another issue. default is 25. when I select 30. it submits the form and does it's thing, but i need to refresh the page to make it display 30 results per page.. how do change that so it displays 30 right after submitting the form?

 

 

I'm guessing that you do the cookie change after the parsing of the page. Try putting the if(isset($_POST['show'])) block before you echo out all of the items/articles/results.

i'm changing the cookie before i echo out any result. it's on the top of the page.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.