{"id":1020,"date":"2014-10-31T13:17:16","date_gmt":"2014-10-31T18:17:16","guid":{"rendered":"http:\/\/xlinesoft.com\/blog\/?p=1020"},"modified":"2022-10-07T13:28:12","modified_gmt":"2022-10-07T18:28:12","slug":"random-page-background-halloween-edition","status":"publish","type":"post","link":"https:\/\/xlinesoft.com\/blog\/2014\/10\/31\/random-page-background-halloween-edition\/","title":{"rendered":"Random page background: Halloween edition"},"content":{"rendered":"<p>Happy Halloween everyone!<\/p>\n<p>Just a little Halloween fun project. To make your website more interesting you can choose to display a random background image for each visitor. Here is how it&#8217;s going to look:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2014\/10\/scr.png\" alt=\"\" title=\"scr\" width=\"749\" height=\"601\" class=\"aligncenter size-full wp-image-1021\" srcset=\"https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2014\/10\/scr.png 749w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2014\/10\/scr-300x240.png 300w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2014\/10\/scr-150x120.png 150w, https:\/\/xlinesoft.com\/blog\/wp-content\/uploads\/2014\/10\/scr-400x320.png 400w\" sizes=\"auto, (max-width: 749px) 100vw, 749px\" \/><\/p>\n<p><!--Check this <a href=\"http:\/\/xlinesoft.com\/livedemo\/backgrounds\/login.php\">live demo<\/a>. Logon as test\/test then logout to see a new background. We save image name in session variable so backround image doesn't change unless user logs out. --><\/p>\n<p>How to add this functionality to your project.<\/p>\n<p>1. <a href=\"http:\/\/xlinesoft.com\/livedemo\/backgrounds\/backgrounds.zip\">Download a set of sample images<\/a> and unzip it to the output folder keeping directory structure. This should create a folder named <strong>backgrounds<\/strong> with a few images in there. You are free to add more images or to remove any you don&#8217;t need. <\/p>\n<p>2. In PHPRunner proceed to Visual Editor, open Header file, switch to HTML mode and paste the following code there. <\/p>\n<pre name=\"code\" class=\"php:nocontrols\">&lt;?php\r\nif (!$_SESSION[\"background\"]) {\r\n\t$arr = array_diff(scandir(\"backgrounds\"), array('..', '.'));\r\n\t$key = array_rand($arr);\r\n\t$_SESSION[\"background\"]=$arr[$key];\r\n}\r\necho('\r\n&lt;style type=\"text\/css\">\r\nbody {\r\nheight:100%;\r\nbackground: transparent url(\"backgrounds\/'.$_SESSION[\"background\"].'\") no-repeat center center fixed;\r\nbackground-size:cover;\r\n}\r\n&lt;\/style>');\r\n?><\/pre>\n<p>3. And here is the relevant code for ASPRunner.NET<\/p>\n<p>For this code to work you need to run the latest ASPRunner.NET build. Paste it to header file in Visual Editor (HTML mode). Images also need to reside in &#8216;bacgrounds&#8217; folder. For those who is interested header file is .cshtml. More info <a href=\"http:\/\/www.asp.net\/web-pages\/overview\/getting-started\/introducing-razor-syntax-%28c%29\">here<\/a>.<\/p>\n<pre name=\"code\" class=\"csharp:nocontrols\">\r\n@{ \r\nvar file=\"\";\r\nif (!XSession.Session[\"file\"]) {\r\n\tvar rand = new Random();  \r\n\tvar files =  Directory.GetFiles(Server.MapPath(webRootPath+\"\/backgrounds\"),\"*.*\");  \r\n\tfile = files[rand.Next(files.Length)];  \r\n\tfile = Path.GetFileName(file);  \r\n\tXSession.Session[\"file\"]=file;\r\n}\r\nelse {\r\n\tfile=XSession.Session[\"file\"];\r\n}\r\n\r\nvar url=@Href(\"~\/backgrounds\/\"+file);\r\n\r\nvar text = String.Format(\"body {{ height:100%; background: transparent url({0}) no-repeat center center fixed; background-size:cover; }}\", url);\r\n\r\n@(Html.Raw(\"<style>\"));\r\n@text\r\n@(Html.Raw(\"<\/style>\"));\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Happy Halloween everyone! Just a little Halloween fun project. To make your website more interesting you can choose to display a random background image for each visitor. Here is how it&#8217;s going to look: How to add this functionality to your project. 1. Download a set of sample images and unzip it to the output folder keeping directory structure. This should create a folder named backgrounds with a few images in there. You are free to add more images or to remove any you don&#8217;t&#8230;<span class=\"clearfix clearfix-post\"><\/span><a href=\"https:\/\/xlinesoft.com\/blog\/2014\/10\/31\/random-page-background-halloween-edition\/\" class=\"more-link\">Continue Reading <span class=\"screen-reader-text\">&#8220;Random page background: Halloween edition&#8221;<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16,1,4,3,8],"tags":[],"class_list":["post-1020","post","type-post","status-publish","format-standard","hentry","category-asp-net","category-php-category","category-php-code-generator","category-php-form-generator","category-tutorials"],"_links":{"self":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1020","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/comments?post=1020"}],"version-history":[{"count":10,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1020\/revisions"}],"predecessor-version":[{"id":2800,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/posts\/1020\/revisions\/2800"}],"wp:attachment":[{"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/media?parent=1020"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/categories?post=1020"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/xlinesoft.com\/blog\/wp-json\/wp\/v2\/tags?post=1020"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}